我想让我的玩家和目标太空舱相撞。这个动作应该摧毁太空舱,并给玩家增加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++都不起作用。
发布于 2018-05-22 04:46:21
那么,首先试着检查您的播放器,您在播放器中使用的对撞机类型是什么?确保您在对撞机组件中检查触发器并在其中添加刚体。太空舱物体必须有刚体在上面。希望能帮上忙。如果要使用触发器,请使用OnTriggerEnter(对撞机对撞机)。如果没有检查触发器,则冲突输入工作
https://stackoverflow.com/questions/50459465
复制相似问题