- Home /
Player movement wrong akse
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)
Comment
Best Answer
Answer by Muuskii · Sep 24, 2012 at 07:25 PM
You're using Vector3.forward instead of using transform.forward
The first one is global, the second one is local. Honestly you could just be using
Input.GetAxis("Horizontal"); //This gives you left and right buttons
and
Input.GetAxis("Vertical"); //This gives you forward and back buttons
Your answer
![](https://koobas.hobune.stream/wayback/20220613081431im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
cant find a solution 1 Answer
Problem with camera and character controller (fighting game) 1 Answer
Player movement 0 Answers
How can I get player to move when only touching an object 0 Answers