【问题标题】:Unity2D Asteroids style gameUnity2D 小行星风格游戏
【发布时间】:2014-02-10 15:50:35
【问题描述】:

我正在尝试在 Unity 中构建一个小行星风格的游戏,并且确实需要一些帮助。我相信就船舶运动而言,我所有的数学都是正确的,但我无法让它在 Unity 中工作。我有几个不同的问题。

  1. 船不会随着速度更新(如果你开始移动然后松开,它会静止不动)

  2. 我不确定在 Unity 中如何将船舶旋转设置为我的特定角度。

任何帮助将不胜感激。

public class playerController : MonoBehaviour {

    public static float timer;
    public static bool timeStarted = false;

    Vector2 accel;
    Vector2 velocity;

    float direction;
    float angle;
    float shotCooldown;
    float speed;

    const float pi = 3.141592f;
    const float maxSpeed = 300;
    const float maxAccel = 500;

    void Start () {
        timeStarted = true;
    }

    void Update () {

        if (timeStarted == true) {
            timer += Time.deltaTime;
        } 

        shotCooldown -= (timer%60);

        angle = direction * pi / 180;

        if (Input.GetKey(KeyCode.W)) {
            accel.y = -Mathf.Cos(angle) * maxAccel;
            accel.x = Mathf.Sin(angle) * maxAccel;
            velocity += accel * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.S)) {
            accel.y = -Mathf.Cos(angle) * maxAccel;
            accel.x = Mathf.Sin(angle) * maxAccel;
            velocity -= accel * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.Space)) {
            if (shotCooldown <= 0)
            {
                // Create new shot by instantiating a bullet with current position and angle
                shotCooldown = .25f;
            }
        }

        if (Input.GetKey(KeyCode.D)) {
            direction += 360 * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.A)) {
            direction -= 360 * Time.deltaTime;
        }
        /*
        if (this.transform.position.x >= Screen.width && velocity.x > 0) {
            this.transform.position.x = 0;
        }*/

        while (direction < 0) {
                direction += 360;
        }
        while (direction > 360) {
                direction -= 360;
        }

        speed = Mathf.Sqrt( (velocity.x * velocity.x) + (velocity.y * velocity.y));

        if (speed > maxSpeed) {
                Vector2 normalizedVector = velocity;
                normalizedVector.x = normalizedVector.x / speed;
                normalizedVector.y = normalizedVector.y / speed;
                velocity = normalizedVector * maxSpeed;
        }

        this.transform.position = velocity * Time.deltaTime;

        transform.rotation = Quaternion.AngleAxis(0, new Vector3(0,0,angle));

    }
}

【问题讨论】:

    标签: c# unity3d game-physics


    【解决方案1】:

    按你现在的方式设置位置通常是个坏主意,因为你实际上并没有使用任何物理。你这样做的方式,velocity 是船的新位置,而不是速度。一旦你松开按键,它就会停止计算新的位置,从而停止移动。

    有几个替代方案可以获得更好的结果:

    1) 一种方法是调用:transform.Translate(Vector3.forward * speed * Time.deltaTime)Vector3.forward 应该对应于您认为“前进”的方向,但如果不是,您可以将其更改为任何可行的方向(例如 Vector3.up) .这意味着您只需要计算一个速度,然后让 Unity 解决剩下的问题。

    2) 如果您在船上使用刚体,您可以简单地执行以下操作: rigidbody.AddForce(Vector3.forward * speed * Time.deltaTime) 它将自动以您给定的速度向给定方向加速船。

    至于轮换,不妨试试这样:

    if (Input.GetKey(KeyCode.D)) 
    {
        Vector3 newRotation = transform.rotation.eulerAngles;
        newRotation.z += 10;
        transform.rotation = Quaternion.Euler (newRotation);
    }    
    else if (Input.GetKey(KeyCode.A)) 
    {
        Vector3 newRotation = transform.rotation.eulerAngles;
        newRotation.z -= 10;
        transform.rotation = Quaternion.Euler (newRotation);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多