猿问

unity OnTriggerEnter

我希望我的 Player 与对象胶囊发生碰撞。这个动作应该摧毁胶囊并为玩家增加一个 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++两者都不起作用。


收到一只叮咚
浏览 126回答 1
微课
1回答
随时随地看视频慕课网APP
我要回答