# Getting player cube to move left or right in relation to camera based on camera's rotation

Right now I'm trying to make the player cube move left, right, up and down in relation to the camera's rotation and position. I figured what I could do is create an empty game object, make the main camera the parent, and then get the vector pointing from the camera's child to another game object that follows the cube's exact position, and that would give me the direction the cube should roll towards.

I have my example in profile view, but in 3D view the camera should be facing the cube with the cube at the center.

So I have the following line of code code:

Vector3 forwardVector = (position1.transform.position - position2.transform.position).normalized;


If you look at the image on the right hand side, I Debug.GetLine'd the resulting vector from the cube's position and ended up with a vector pointing down. In theory though, it should be pushing off to the right hand side. Here's the rest of the code:

Vector3 perpendicularVector = Vector3.Cross (Vector3.up, resultant).normalized;

if (Input.GetKey (KeyCode.UpArrow))
{
}
if (Input.GetKey(KeyCode.DownArrow))
{
}
if (Input.GetKey (KeyCode.LeftArrow))
{
}
if (Input.GetKey (KeyCode.RightArrow))
{
}


As a result, when I press the up arrow key, I end up with the cube flying upwards toward the sky, and the down key brings it back down towards the plane, but my intention is for the vector to move along the XZ plane.

Also, with this line of code, I was hoping to get the line perpendicular to my intended vector and Vector3.up, but I naturally get wonky results from that.

Vector3 perpendicularVector = Vector3.Cross (Vector3.up, resultant).normalized;


If there's an easier method to achieving my goals, that's fine, but it would be nice too to figure out why the vector is pointing down instead of to the right.

It's also might be worth noting that the camera already successfully rotates around the player cube.

• The mathematical way to accomplish this is to transform the direction. I don't have time right now to explain further, but lookup stuff like docs.unity3d.com/ScriptReference/… May 22, 2017 at 19:40

Create a 4x4 rotation matrix:

Matrix4x4 rotation = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(yourRotation), Vector3.one);


Then take the forward vector and multiply it with the rotation matrix:

Vector3 rotatedForward = rotation.multiplyVector(Vector3.forward);


Getting the vector that points left in the player's coordinate system can be simply achieved by using the vector (0, -1, 0) (Vector3.left) and rotating it with the rotation vector.

To move the object, just add/subtract the vectors from the current position of the object or add them as a force.

• I'm sorry, I'm a bit new to unity so there are still some concepts I don't quite understand. Could you translate your explanation into code? I'm not sure what you mean by "using the vector (0, -1, 0) (Vector3.left) and rotating it with the rotation vector. And when you say add/subtract the vectors from the current position, do you mean getting the XZ values from the position of the camera to the object and getting a vector from that? I can understand code, but I can't translate explanations into code very well. Still trying!
– cc42
Jan 17, 2017 at 18:22
• @Balsa There's nothing I can say, almost everything you need is in the answer in a code form. You may need to do what I did for the forward vector but instead using the predefined Vector3.left vector. Jan 17, 2017 at 19:41
• Alrighty, I'll try playing around with it. Thanks!
– cc42
Jan 17, 2017 at 20:03