# How to do Local Space Rigidbody.Velocity

Hi, I am attempting to make a third person shooter right now, I have gotten my camera and character movement setup however I am having issues trying to convert Rigidbody.Velocity into local space so it will move in the same direction as my player is facing. I am currently using the new input system which has made it pretty hard to really find an answer on how to do this correctly, any help would be appreciated!

I am also aware that some may ask why I am using velocity instead of addforce, it’s because I think it feels a lot better than addforce.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.InputSystem;

public class MovementController : MonoBehaviour
{
public InputAction moveAction;

public Rigidbody rb;

public float speed = 10f;
public float rotateSpeed = 1;

private Vector2 input;
private Vector2 look;

private Vector3 movement;

void OnEnable()
{
moveAction.Enable();
Cursor.lockState = CursorLockMode.Locked;
}

public void Update()
{
movement = new Vector3(-input.x, 0, -input.y) * speed;
movement.y = rb.velocity.y;
transform.Rotate(0, look.x, 0);
}

public void FixedUpdate()
{
rb.velocity = movement;
}

public void onLook(InputAction.CallbackContext context)
{
}
}
``````

This should work if there’s no built-in way to do the same thing:

``````Vector3 movement = RotateXY(input, Mathf.Deg2Rad * Camera.main.transform.rotation.eulerAngles.y).normalized;

// convert it for your velocity
movement.x = -movement.x;
movement.z = -movement.y;
movement *= speed;
movement.y = rb.velocity.y;

// put this wherever
public static Vector3 RotateXY(Vector3 point, float radians)
{
float x = point.x;
float y = point.y;
return new Vector3(
x * c + y * s,
-x * s + y * c,
0
);
}
``````

Note: using Camera.main isn’t good practice, there’s allegedly a performance hit - so if you want to optimize this once it’s working, use a reference like `private Camera cam;` then in Awake call `cam = Camera.main`, then use cam.transform instead of Camera.main.transform.

1 Like

After a bit of tweaking I have gotten this working however there is a strange bug that is going on. My movement was originally inverted hence the reason I inverted them again by making the input negative. However after using your method it fixes the inverted controls now meaning setting it to negative gives me inverted controls. So my first thought on fixing this was just removing these lines:

``````        movement.x = -movement.x;
movement.z = -movement.y;
``````

However doing that makes it so the player can only move in the x direction for some reason. The weird solution I found to this issue was setting the input as negative in the movement variable and then also keeping the previously lines which solved this issue.

I have put an imgur album together to better show what I am talking about: Imgur: The magic of the Internet

While I would be more than fine to be using my weird work around for this project for now I think it would be really beneficial if I were to know why it is acting this way. Any help figuring this one out too would be great!

``````// you still need to map y to z
movement.z = movement.y;
``````
1 Like

Oh that makes a lot of sense, I really appreciate your help!

1 Like