I’ve tried so many things but nothing is working. How do I make my character move based on the local axis and not the global axis?
Here is the code:
private void FixedUpdate()
{
Vector3 myVelocity = new Vector3((HorizontalInput * Speed), 0f, (ForwardInput * Speed));
if (VerticalInput == 1)
{
playerRb.velocity = new Vector3(myVelocity.x, VerticalInput * VerticalPower, myVelocity.z);
} else
{
playerRb.velocity = new Vector3(myVelocity.x, playerRb.velocity.y, myVelocity.z);
}
}
You have to convert your local vector first. To do that, use:
globalV3 = transform.TransformDirection(localV3)
Then you can use rb.velocity(globalV3).
anszwa
3
Hello, if I adopt your code, it could look something like this:
private void FixedUpdate()
{
Vector3 myVelocity = new Vector3((HorizontalInput * Speed), 0f, (ForwardInput * Speed));
playerRb.velocity = Quaternion.Euler(playerRb.transform.eulerAngles) * myVelocity;
}
Unity’s Quaternion class has operator overloading, multiplying a quaternion and a vector3 returns a vector3 rotated by the value of the quaternion.