AK824
AK824

Reputation: 217

Unity2D Asteroids style game

I am trying to build an asteroids style game in Unity and could really use some help. I believe all my math is correct as far as the ship movement but I am having trouble getting it to work inside Unity. I am having a couple different problems.

  1. The ship does not update with velocity ( if you start moving and then let go, it will stand still)

  2. I am unsure in Unity how to set the ships rotation to my specific angle.

Any help would be greatly appreciated.

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

    }
}

Upvotes: 2

Views: 3174

Answers (1)

Steven Mills
Steven Mills

Reputation: 2381

It's usually a bad idea to set the position the way you are, because you're not actually using any physics. The way you're doing it, velocity is a new position for the ship, not a speed. Once you let go of the keys, it stops calculating new positions, and thus stops moving.

There are a couple of alternatives which would make for a better result:

1) One way this can be done is by calling: transform.Translate(Vector3.forward * speed * Time.deltaTime) Vector3.forward should correspond to the direction you consider as "forward", but if not, you can change it to whichever works (eg Vector3.up). This means you only really need to calculate a speed and let unity hangle the rest.

2) If you're using a rigidbody on your ship, you could simply do: rigidbody.AddForce(Vector3.forward * speed * Time.deltaTime) which will automatically accelerate the ship in the given direction by whatever speed you give it.

As for rotation, perhaps try something like this:

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

Upvotes: 1

Related Questions