how to make player movement be based on direction its facing
I want my player to move directions relative to the direction the player is facing when I press the arrow keys. So if I press the "up" arrow to move the player forward, I want forward to be the direction it is facing, yet forward seems to always be tied to the worlds forward, because pressing the "up" arrow moves me in the same direction regardless of what way I'm facing. My code does correctly turn the player so that it is facing whichever direction it is going, making the local "forward" correct, but then pressing the "up" arrow turns my player to the Global forward instead of moving in the players forward. I'm pretty new to both coding and Unity, and I don't know how to go about changing that. Any advice or info is appreciated (even if it doesn't solve this particular problem, I'm eager to learn) but I will say I'd prefer not to use a Rigidbody, I'm trying to learn using character controller. Here is my code for the player:
public class PlayerController : MonoBehaviour { private CharacterController playerCC; private Vector3 playerVelocity; private bool groundedPlayer; private float playerSpeed = 10.0f; private float jumpHeight = 3.0f; private float gravityValue = -9.81f; [SerializeField] Camera mainCamera;
private void Start()
{
playerCC = gameObject.GetComponent<CharacterController>();
}
void Update()
{
groundedPlayer = playerCC.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 move = new Vector3(horizontalInput, 0, verticalInput);
playerCC.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
playerCC.Move(playerVelocity * Time.deltaTime);
}
}
Answer by tylerdtrudeau · Mar 05, 2021 at 05:05 AM
Im just gonna guess you wand the sideways arrow key to rotate the player? then up and down to move forward and back relative to facing direction?
public float movementSpeed = 25f;
void Update()
{
float hInput = Input.GetAxis("Horizontal");
float vInput = Input.GetAxis("Vertical");
if (vInput > 0)
{
transform.position += transform.up * Time.deltaTime *movementSpeed;
}
if (vInput < 0)
{
transform.position -= transform.up * Time.deltaTime *movementSpeed;
}
if (hInput > 0)
{
transform.Rotate(0, 0, -1f, Space.Self);
}
if (hInput < 0)
{
transform.Rotate(0, 0, 1f, Space.Self);
}
Thanks, but that isn't quite what I was looking for. My game is 3D, 3rd person perspective so I need the arrows to move left, right, forward, and back, but I want those directions to be relative to the direction the player/camera is facing. Right now, "forward" is always the same direction regardless of where the player is facing.
Well most of the code I provided should still work. The rotation should still be fine (you may need to change which axis the vector is using (0,01,) or (0,1,0) but the transform.rotate will still work in 3D space on a 3D game object (player). For the forward and back it SHOULD work as well but instead of Vector3.up, try Vector3.Forward.
Ok I tested it out and it seems to work perfect. So here you go.
public class Movement : MonoBehaviour
{
public float movementSpeed = 5f;
void Update()
{
float hInput = Input.GetAxis("Horizontal");
float vInput = Input.GetAxis("Vertical");
if (vInput > 0)
{
transform.position += transform.forward * Time.deltaTime *movementSpeed;
}
if (vInput < 0)
{
transform.position -= transform.forward * Time.deltaTime *movementSpeed;
}
if(hInput > 0)
{
transform.Rotate(0, 1f, 0, Space.Self);
}
if (hInput < 0)
{
transform.Rotate(0, -1f, 0, Space.Self);
}
}
}
then attach this next script onto your camera
public class CameraFollow : MonoBehaviour
{
public GameObject player;
float camDistance = 10f;
void LateUpdate()
{
transform.position = player.transform.position - player.transform.forward * camDistance;
transform.LookAt (player.transform.position);
transform.position = new Vector3 (transform.position.x, transform.position.y + 5, transform.position.z);
}
}
go into the editor and clcik on your camera, drag your player from the hierarchy into the "player" field of the CameraFollow script and youre done.
Thank you!!! It's not quite what I want yet, but I was able to incorporate your code to get much closer, now to fiddle around and see if I can get it just right.
Your answer
Follow this Question
Related Questions
Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 0 Answers
is it possible to edit the Continuous Move provider (Action based) to add a jump feature? 0 Answers
How to move my rigid body in the direction my model is facing. 0 Answers
Problem with Jump script 1 Answer
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers