Hej.
I have a problem where my player (a cube) is moving out of the wrong akse. When I start my game it’s going fine but when I rotate it and move forward it’s going out of the global akse and not the local like I want.
Here is the code I’m using to move my player:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour
{
//Public variables
public int PlayerMovementSpeed = 1;
public int PlayerRotationSpeed = 1;
//Private variables
Vector3 currentDirrection = new Vector3(0,0,0);
void FixedUpdate ()
{
currentDirrection = new Vector3(0,0,0);
//Going forward on Input "Forward"
if(Input.GetButton("Forward"))
{
currentDirrection += Vector3.forward;
}
//Going backward on Input "Backward"
if(Input.GetButton("Backward"))
{
currentDirrection += Vector3.back;
}
//Turning to the right on Input "Right"
if(Input.GetButton("Right"))
{
rigidbody.angularVelocity += Vector3.up * PlayerRotationSpeed;
}
//Turning to the left on Input "Left"
if(Input.GetButton("Left"))
{
rigidbody.angularVelocity += Vector3.down * PlayerRotationSpeed;
}
rigidbody.velocity = currentDirrection * PlayerMovementSpeed;
}
}
Thanks in advanse (I’m pretty new to unity so be gentle)