Unity2D小行星风格游戏

时间:2014-01-21 07:14:54

标签: c# unity3d game-physics

我正在尝试在Unity中构建一个小行星风格的游戏,并且可以真正使用一些帮助。我相信我的所有数学运算都是正确的,但我无法让它在Unity内部工作。我有几个不同的问题。

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

  2. 我不确定如何将船只旋转设定到我的特定角度。

  3. 非常感谢任何帮助。

    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));
    
        }
    }
    

1 个答案:

答案 0 :(得分:1)

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

有几种方法可以带来更好的结果:

1)这样做的一种方法是通过调用:transform.Translate(Vector3.forward * speed * Time.deltaTime) Vector3.forward应该对应你认为是“前进”的方向,但如果没有,你可以将它改为任何有用的方法(例如Vector3。向上)。这意味着你只需要计算一个速度,让其他人团结一致。

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);
}
相关问题