【问题标题】:Wrong tag at collision number 2碰撞编号 2 处的错误标签
【发布时间】:2020-04-03 21:50:24
【问题描述】:

您好,我正在制作一个 2d 游戏,我有一个玩家需要与 2 个对象发生碰撞。第一个对象给了他更多的健康,并带有“加电”标签并且可以工作,但第二个带有“伤害”标签的对象不起作用。我应该在脚本中更改什么?这里是“PowerUp”的脚本,这里是播放器的脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class PowerUpDetector : MonoBehaviourPun
{
    // reference this via the Inspector
    [SerializeField] private Character healthbar;
    [SerializeField] private Character health;
   

    private void Awake()
    {
        if (!healthbar) healthbar = GetComponent<Character>();
        
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        // or whatever tag your powerups have
        if (!other.CompareTag("PowerUp"))
        {
            Debug.LogWarning($"Registered a collision but with wrong tag: {other.tag}", this);
            return;
        }
        
        var powerup = other.GetComponent<PowerUp>();
        if (!powerup)
        {
            Debug.LogError($"Object {other.name} is tagged PowerUp but has no PowerUp component attached", this);
            return;
        }

        Debug.Log("Found powerup, pick it up!", this);
        powerup.Pickup(healthbar);
        if (!other.CompareTag("Hurt"))
        {
            if (photonView.IsMine)
            {
                photonView.RPC("Damage", RpcTarget.All);
                
            }

            
        }



    }
    [PunRPC]
    void Damage()
    {
        health -= 20;
    }

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using Photon.Pun;
using Photon.Realtime;
using Photon;
using UnityEngine.UI;


public class Character : MonoBehaviourPun,IPunObservable
{

    Rigidbody2D rb;
    float dirX;

    [SerializeField]
    float moveSpeed = 5f, jumpForce = 400f, bulletSpeed = 500f;

    [SerializeField] private float health = 100;
    [SerializeField] private Slider slider;
    [SerializeField] private Gradient gradient;
    [SerializeField] private Image fill;

    Vector3 localScale;
   
    public Transform barrel;
    public Rigidbody2D bullet;

    // Use this for initialization
    void Start()
    {
        localScale = transform.localScale;
        rb = GetComponent<Rigidbody2D>();
        if (photonView.IsMine)
        {
            
            
        }
        else
        {

        }
    }
    public float Health
    {
        get { return health; }
        set
        {
            health = value;
            slider.value = health;
            fill.color = gradient.Evaluate(slider.normalizedValue);
        }
    }

    private void OnCollisionEntered2D(Collision2D col)
    {
        if( col.gameObject.CompareTag ("Hurt"))
        {
            if (photonView.IsMine)
            {
                photonView.RPC("Damage", RpcTarget.All);
            }
        }
    }
    [PunRPC]
    void Damage()
    {
        health -= 20;
    }
    // Update is called once per frame
    void Update()
    {
        if (photonView.IsMine)
        {
            dirX = CrossPlatformInputManager.GetAxis("Horizontal");
            if (dirX != 0)
            {
                barrel.up = Vector3.right * Mathf.Sign(dirX);
            }

            if (CrossPlatformInputManager.GetButtonDown("Jump"))
                Jump();

            if (CrossPlatformInputManager.GetButtonDown("Fire1"))
                Fire();
            
        }
        else
        {

        }
    }

    void FixedUpdate()
    {
        if (photonView.IsMine)
        {
            rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
        }
    }




    void Jump()
    {
        if (photonView.IsMine)
        {
            if (rb.velocity.y == 0)
                rb.AddForce(Vector2.up * jumpForce);
        }
    }

    void Fire()
    {
        if (photonView.IsMine)
        {
            var firedBullet = Instantiate(bullet, barrel.position, barrel.rotation);
            firedBullet.AddForce(barrel.up * bulletSpeed);
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(health);
        }else if (stream.IsReading)
        {
           health = (float)stream.ReceiveNext();
        }
    }
    public void SetMaxHealth(int value)
    {
        if (photonView.IsMine)
        {
            slider.maxValue = value;
            // The property handles the rest anyway
            Health = value;
        }

    }
}

我在运行状况 -= 20 (operator -= can't be applied) 的“Power Up Detector”中出现错误,操作数类型为“Character”和“int”。

【问题讨论】:

    标签: unity3d collider


    【解决方案1】:

    我认为您将不得不使用health.Health。您的变量 health 指的是 Character 类。当您想更改健康状况时,您必须改用该类的属性。所以试试:

    health.Health -= 20;

    我建议将 health 变量重命名为与 Character 类相关的其他名称。

    【讨论】:

    • 它仍然在控制台中向我显示:注册了一个冲突但标签错误:Hurt UnityEngine.Debug:LogWarning(Object, Object)
    • 我该怎么办? @kokosbrood。
    • @raluca2,如果我理解正确的话,你有两个你的玩家应该击中的物体。现在,如果我查看您的代码,它会检查它是否命中了PowerUp 标签。如果不是,它将给出错误消息并退出您的函数。稍后编写一些代码,检查标签是否为hurt。这永远不会达到(您在PowerUp 检查退出)。我会使用一个开关功能,例如:打开标签;然后在其中添加两种情况。有关使用字符串的示例,请参阅 [link]csharp.net-informations.com/statements/csharp-switch-case.htm[/…
    • 道歉。此评论框中的不同格式。 C# switch case。您必须直接使用.tag 属性。
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2014-04-14
    相关资源
    最近更新 更多