Question by
Alureus · Jul 26, 2017 at 05:53 PM ·
rotationmovementmovement script
How to make player go forward when he is rotated?
I have a flying player model, and i can get him to rotate, but when he rotates in the air, if i press forward, he still goes just one direction and not the direction he is facing. How can i make him go forward towards where he is facing?
My movement code:
public class PlayerController : MonoBehaviour
public float speed;
public float rotationspeed;
public float rotation;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKey(KeyCode.E))
// Rotate the object around its local X axis at 1 degree per second
transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed);
transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
if (Input.GetKey(KeyCode.Q))
transform.Rotate(Vector3.down * Time.deltaTime * rotationspeed);
transform.Rotate(Vector3.down * Time.deltaTime, Space.World);
if (Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpSpeed);
}
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Vector3 rotation = new Vector3(0.0f, moveHorizontal, 0.0f);
rb.AddForce(movement * speed);
}
public float jumpSpeed = 5f;//or whatever you want it to be
public Rigidbody rb; //and again, whatever you want to call it
Comment