Reputation: 3
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);
}
}
Upvotes: 0
Views: 5470
Reputation: 2161
Is this what your're asking?
[yourigidbody].velocity = transform.forward * speed;
or
[yourigidbody].velocity = transform.TransformDirection(Vector3([xspeed], [yspeed], [zspeed]));
Upvotes: 3