- Home /
How do i make local velocity?,How do i make velocity move the player the way the object is facing?
Hi I'm trying to make a player movement script using rigidbodies.I'm also pretty new to this so sorry if I have some mistakes or weird things with my code.I've decided to use velocity to move my player and I've set it up as well as camera rotation based on the mouse axis. (This also rotates the player)
But I noticed that even though my player rotates the way that I want him he doesn't really move the way he is facing. So bottom line is I was wondering how do I make him move locally based on the direction he is facing?
Here is my code:
// Sets the rigidbody for the player
public Rigidbody rb;
// Sets a modifiable player speed
public float speed = 0.0f;
// Makes a empty vector for movement for forward,back,left,and right
public Vector3 movement;
// Checks if player is grounded
public bool isGrounded;
// calls the camera
public GameObject icu;
// Mouse sensitivity
public float sensitivity = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// sets the movement and direction based on the axis of the keys
movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
if (Input.GetAxis("Mouse X") < 0)
{
transform.Rotate(Vector3.up * -sensitivity * Time.deltaTime);
}
if (Input.GetAxis("Mouse X") > 0)
{
transform.Rotate(Vector3.up * sensitivity * Time.deltaTime);
}
if (Input.GetAxis("Mouse Y") < 0)
{
icu.transform.Rotate(Vector3.left * -sensitivity * Time.deltaTime);
}
if (Input.GetAxis("Mouse Y") > 0)
{
icu.transform.Rotate(Vector3.left * sensitivity * Time.deltaTime);
}
}
// Used for physics updates
void FixedUpdate()
{
// Calls the module for player movement and sends our movement variable to said module
playerMovement(movement);
}
// Makes a separate module for player movement and takes our module and sets it to help move the player in that direction
void playerMovement(Vector3 direction)
{
rb.velocity = direction * speed;
}
I was also curious if perhaps there was a good way to add a jump to the way I'm doing this?
,Hi, I'm new to most of this stuff but I'm using a rigidbody for my player and I've got it set up where the camera and object move based on the mouse axis. I also have it set up to where my player moves with velocity but it looks like it only moves globally. How can I change this to work local to the player's direction?
Here is my code:
// Sets the rigidbody for the player
public Rigidbody rb;
// Sets a modifiable player speed
public float speed = 0.0f;
// Makes a empty vector for movement for forward,back,left,and right
public Vector3 movement;
// Checks if player is grounded
public bool isGrounded;
// calls the camera
public GameObject icu;
// Mouse sensitivity
public float sensitivity = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// sets the movement and direction based on the axis of the keys
movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
if (Input.GetAxis("Mouse X") < 0)
{
transform.Rotate(Vector3.up * -sensitivity * Time.deltaTime);
}
if (Input.GetAxis("Mouse X") > 0)
{
transform.Rotate(Vector3.up * sensitivity * Time.deltaTime);
}
if (Input.GetAxis("Mouse Y") < 0)
{
icu.transform.Rotate(Vector3.left * -sensitivity * Time.deltaTime);
}
if (Input.GetAxis("Mouse Y") > 0)
{
icu.transform.Rotate(Vector3.left * sensitivity * Time.deltaTime);
}
}
// Used for physics updates
void FixedUpdate()
{
// Calls the module for player movement and sends our movement variable to said module
playerMovement(movement);
}
// Makes a seprate module for player movement and takes our module and sets it to help move the player in that direction
void playerMovement(Vector3 direction)
{
rb.velocity = direction * speed;
}
Answer by tormentoarmagedoom · Apr 01, 2020 at 02:15 PM
Hello.
There are 2 ways of calculate vectors in Unity, Local and World
If you calculate the movement vector using World coordinates, player will olways move same direction regardless its rotation.
Vector3 direction = new Vector3(1,0,0);
But if you calculate it using Local coords, the vector ("ahead") is where the object is face (the blue arrow on the Editor)
Vector3 direction = SomeObject.transform.forward
https://docs.unity3d.com/ScriptReference/Transform-forward.html
Bye!
Your answer
Follow this Question
Related Questions
Adding force relative to player's rotation with rigidbody.velocity 1 Answer
Can you pls help me fix this rigidbody collision problem 0 Answers
Issue with Instantiate and the instant that spawns on a moving object that rotates 1 Answer
Is rigibody velocity frame rate independent? 1 Answer
Rigidbody physics - equalizing force 1 Answer