0

I have just started using Unity and I decided to make a 2D asteroids clone, and so far I have the rotation and movement down but I can't seem to figure out how to make the ship save its velocity (so you continue moving until you give an equal force in the opposite direction) ... This is what I have so far...

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour {

void Update () {
    GameObject facer = GameObject.Find("facer");
    //Facer is another object I am using as a sort of aiming reticle

    if(Input.GetKey(KeyCode.UpArrow))
    {
        this.transform.position = Vector3.MoveTowards(this.transform.position, facer.transform.position, 0.1f);
    }
    if(Input.GetKey(KeyCode.DownArrow))
    {
        this.transform.position = Vector3.MoveTowards(this.transform.position, facer.transform.position, -0.1f);
    }
    if(Input.GetKey(KeyCode.LeftArrow))
    {
        RotateLeft();
    }
    if(Input.GetKey(KeyCode.RightArrow))
    {
        RotateRight();
    }
}
void RotateLeft() {
    transform.Rotate (Vector3.back * -5f);
}
void RotateRight() {
    transform.Rotate (Vector3.forward * -5f);
}

}

1 Answer 1

3

Is this what your're asking?

[yourigidbody].velocity = transform.forward * speed;

or

[yourigidbody].velocity = transform.TransformDirection(Vector3([xspeed], [yspeed], [zspeed]));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.