- Home /
Player movement question (Unity3D)
Hi there, so I have a simple Unity 3D Player Movement Script. It works well, I can go left, right, forward, back etc. I also have a first person script for looking around which also works well, but some problems arise when I turn around and try use the player movement script. Everything seems to go inversed (Example, forward brings you backwards, vice versa) I was wondering if there was a problem with my code or a simple fix. All the best!
void FixedUpdate ()
{
if (rb.velocity.y == 0)
isGrounded = true;
if (rb.velocity.y > 0.1)
isGrounded = false;
if (Input.GetKey(KeyCode.W))
rb.AddForce (Vector3.forward * MoveSpeed);
if (Input.GetKey(KeyCode.S))
rb.AddForce (Vector3.back * MoveSpeed);
if (Input.GetKey(KeyCode.D))
rb.AddForce (Vector3.right * MoveSpeed);
if (Input.GetKey(KeyCode.A))
rb.AddForce (Vector3.left * MoveSpeed);
if (Input.GetKeyDown(KeyCode.Space))
rb.AddForce (Vector3.up * JumpForce);
}
}
Answer by metalted · Mar 17, 2020 at 08:05 PM
This is because you are using the world coordinate system. You can think of it this way: You are sitting in a car and you are driving in the z direction. Your head is facing forward and everything is fine. When you look out of the left window, do you expect the car to go that way too? Nope, its still heading in the z direction. That is what your code is doing too.
What you NEED, is that the body move relative to the heads coordinate system. So if you look out of the left window, and hit the gas, the car should go sideways, how stupid that may sound :P You can achieve this by using transform.TransformDirection(). Simple example:
Transform head;
Rigidbody body;
body.AddForce(head.TransformDirection(Vector3.forward * moveSpeed);
You should check out these pages: https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html
Hope this gets you moving the right way!
Hi, sorry but I'm still kind of confused on what to do with it. How would I apply it to my code to get up and running? If needed, here is my First person looking around script that I have applied to my player gameobject using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraScript : $$anonymous$$onoBehaviour
{
public float SensitivityX = 15f;
public float SensitivityY = 15f;
public float $$anonymous$$inimumX = -60f;
public float $$anonymous$$aximumX = 60f;
public float $$anonymous$$inimumY = -360f;
public float $$anonymous$$aximumY = 360f;
float RotationX = 0f;
float RotationY = 0f;
public Camera Cam;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
}
// Update is called once per frame
void Update()
{
RotationY += Input.GetAxis("$$anonymous$$ouse X") * SensitivityY;
RotationX += Input.GetAxis("$$anonymous$$ouse Y") * SensitivityX;
//basically means that it constraints the value between the $$anonymous$$ and max.
//Note we just constrain X rotation, Y rotation can go all the way around and keep going, that's fine
RotationX = $$anonymous$$athf.Clamp(RotationX, $$anonymous$$inimumX, $$anonymous$$aximumX);
//the player we just want to rotate around the Y-axis, nothing around the X or Z
transform.transform.localEulerAngles = new Vector3(0, RotationY, 0);
//But the Camera needs to rotate around both the X and Y axes
Cam.transform.localEulerAngles = new Vector3(-RotationX, RotationY, 0);
}
}
Answer by BBIT-SOLUTIONS · Mar 22, 2020 at 04:36 PM
You could try using transform.forward, transform.up, etc instead of the Vector3 ones.
That should take the local directions instead of the global ones.
Your answer
Follow this Question
Related Questions
How do I disable player input action map and enable the UI action map? 4 Answers
Making Player Rotation Smoother 0 Answers
Translating Player Movement and Clipping Through Walls 2 Answers
Click to move player while avoiding jitter at edges 0 Answers
How can I make it so the player can't move past a certain point? 1 Answer