C# AddForce Relative To Camera Left And Right Movement

All other answers related to AddForce relative to camera position only explain how to add force forward and backward. I’ve got that part down, but I need to be able to move left and right.

Vector3 pushDir = GameObject.Find("cameraMan").transform.position - transform.position;

if (Input.GetKey("up") || Input.GetKey("down"))
        {
            rb.AddForce(pushDir.normalized * Input.GetAxis("Vertical") * (-10));
        }

This code works fine, I already have a script that rotates the camera and this always moves you forwards/backwards from its perspective. But when I try to write this for left/right movement:

if (Input.GetKey("right") || Input.GetKey("left"))
        {
            rb.AddForce(sideDir.normalized * Input.GetAxis("Horizontal") * (-10));
        }

It still only moves forwards and backwards. I’ve tried pushDir.right, pushDir.normalized.right, I even tried making a perpendicular child for the camera, to see if moving back and forth relative to that object would move me left and right; no dice.
Side note: this is a game about moving around a spinning ball, AddForce is what I’d like to use for that movement.
Any help is much appreciated!

push dir is just camera.transform.forwards.
or transform.up.
or transform.right.
or multiply them by -1.0 to go opposite direction…
if you need pushdir to be horizontal… then just copy the vector, zero out the y component, then re-normalize it, to get a flat forwards and sideways push vector.

:slight_smile:

If you must use ( pushDir = targetObjectPosition - cameraPosition).Normalized; … then you can make a side vector like this… side.x = pushDir.z; side.y=0; side.z = -pushDir.x;
happy vectoring !