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