且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的碰撞 2d 在 Unity 上的 C# 中不起作用

更新时间:2022-04-05 21:41:38

using UnityEngine;使用 System.Collections;

using UnityEngine; using System.Collections;

公共类 SquadMovement : MonoBehaviour {

public class SquadMovement : MonoBehaviour {

float constantspeed = 3;
float speed;

//Key inputs

void Update () {

    transform.Translate (constantspeed * Time.deltaTime, 0, 0);
    if (Input.GetKeyDown (KeyCode.D)) {  

        StopAllCoroutines ();
        StartCoroutine (RightMovement(0f));
    }

    if (Input.GetKeyDown (KeyCode.A)) {

        StopAllCoroutines ();
        StartCoroutine (LeftMovement(0f));
    }
}

//Movement itself (Right, Left)

IEnumerator RightMovement (float Rloop) {

    while (transform.position.x < Time.time * constantspeed + 6) {

        speed = 10f;
        transform.Translate (speed * Time.deltaTime, 0, 0);
        yield return new WaitForSeconds (Rloop);
    }

    if (transform.position.x > Time.time * constantspeed + 5.9) {

        StopAllCoroutines ();
        StartCoroutine (LeftMovement (0f));
    }
}

IEnumerator LeftMovement (float Lloop) {

    while (transform.position.x > Time.time * constantspeed -8) {

        speed = -7f;
        transform.Translate (speed * Time.deltaTime, 0, 0);
        yield return new WaitForSeconds (Lloop);
    }
}

}