【问题标题】:Unity OnTriggerEnterunity OnTriggerEnter
【发布时间】:2018-05-22 12:46:47
【问题描述】:

我希望我的播放器与对象胶囊发生碰撞。 这个动作应该破坏胶囊并为玩家添加 10 的速度值。

但是这段代码不起作用:

public class PlayerController : MonoBehaviour {
public KeyCode moveL;
public KeyCode moveR;

public float horizontal = 0;
public int laneNum = 2;
public string controllocked = "n";

public float speed;

void Update ()
{
    GetComponent<Rigidbody>().velocity = new Vector3(horizontal, GM.verticalVelocity, speed);

    if ((Input.GetKeyDown(moveL)) && (laneNum > 1) && (controllocked == "n"))
    {
        horizontal = -2;
        StartCoroutine(StopSlide());
        laneNum = laneNum - 1;
        controllocked = "y";
    }
    else if ((Input.GetKeyDown(moveR)) && (laneNum < 3) && (controllocked =="n"))
    {
        horizontal = 2;
        laneNum = laneNum + 1;
        StartCoroutine(StopSlide());
        controllocked = "y";
    }
}

void OnCollisionEnter(Collision other)
{
    if(other.gameObject.tag == "lethal")
    {
        Destroy(gameObject);
    }
    if (other.gameObject.name == "Capsule")
    {
        Destroy(other.gameObject);
        speed = 10;   
    }
}

IEnumerator StopSlide()
{
    yield return new WaitForSeconds(.5f);
    horizontal = 0;
    controllocked = "n";
}

到目前为止,我尝试过的是 speed += 10speed++ 都不起作用。

【问题讨论】:

  • 更改速度时是否调用Update()?
  • 好的,您可以通过第一次执行此操作来轻松调试。)将speed=10; 替换为此speed+=10;,然后尝试将Debug.Log("The speed now is: " + speed); 放在speed+=10 的下方,然后回到这里
  • 我不想更新每一帧,还是我必须这样做?我只想增加一次速度。我把 debug.log 放在 update coudn't do it under OnTriggerEnter ......这是怎么回事......

标签: c# unity3d


【解决方案1】:

好吧,首先尝试检查您的播放器,您在播放器中使用的碰撞器类型是什么? 确保在对撞机组件中检查触发器并将刚体添加到其中。 胶囊物体上必须有刚体。 希望它有所帮助。 如果要使用 triggerenter,请使用 OnTriggerEnter(Collider collider)。不检查触发器碰撞进入工作

【讨论】:

  • 不客气,不用担心。有时我也忘记了这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
相关资源
最近更新 更多