- Home /
Question by
Jaytuple · May 18, 2020 at 08:48 AM ·
charactercontrollermovement scriptdirectionfacing
Movement based on camera
I have the following code that moves the character, however it moves based on world instead of camera facing. I'm using Cinemachine to rotate the camera throughout the game, so how can I change this to get the character to move in relation to camera?
void Move()
{
float xInput = Input.GetAxis("Horizontal");
float zInput = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(xInput, 0, zInput) * moveSpeed;
dir.y = rig.velocity.y;
rig.velocity = dir;
Vector3 facingDir = new Vector3(xInput, 0, zInput);
if (facingDir.magnitude > 0)
{
transform.forward = facingDir;
}
}
Comment
Did you figure this out? I am tackling this issue as well at the moment. I have a rb that is being moved with addforce. I want to move the ball toward where the camera is pointing.
Answer by wewewu · Oct 26, 2020 at 03:40 PM
if you want to move in local space:
Vector3 dir = new Vector3(xInput * tr.right, rig.velocity.y, zInput * tr.forward) * moveSpeed //transform could be the transform of you camera