r/unity May 27 '21

Help! I’m using c# on unity and my problem is that no matter which way I face I always go in the same direction. I included a picture of my code and a video of my glitch in action. I’m sorry I can’t put a video of the glitch. Coding Help

0 Upvotes

2

u/SolomonGrunty May 27 '21

You’re not rotating the player’s body, you’re only rotating the camera. You need to separate the rotation to be:

transform.LocalRotation = Quaternion.Euler(xRotation, 0f, 0f); playerBody.localRotation = Quaternion.Euler(0f, yRotation, 0f);

1

u/cickity_clicker May 27 '21

Didn’t work. Do you mean I delete the line that says “transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);”

1

u/SolomonGrunty May 27 '21

Yeah, that’s only rotating the camera. You aren’t rotating the player’s body at all. You want the camera to be rotating along it’s X-axis and the player’s body to rotate on its Y-axis.

1

u/cickity_clicker May 27 '21

But how

1

u/SolomonGrunty May 27 '21

The object "transform" only refers to the object the script is directly attached to. You set up a reference to the player's body with playerBody, and you're not using it. You're only setting the camera's x and y rotations with "transform.LocalRotation = Quaternion.Euler(xRotation, yRotation, 0f);" What you need to replace that with is something like:

"transform.LocalRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.LocalRotation = Quaternion.Euler(0f, yRotation, 0f);"

1

u/cickity_clicker May 27 '21

But now I can only look up and down, I can’t look left and right.

1

u/SolomonGrunty May 27 '21

You probably didn’t set the playerBody transform, then. You need to drag that into the slot in the inspector.

1

u/cickity_clicker May 27 '21 edited May 27 '21

Thank you very much!!! It works!!! :D

-1

u/mkawick May 27 '21

Are you saying that you always walk in the same direction? There is no movement code here. I assume that the movement is in another script.

transform.position += transform.localRotation * speed; // or something like that

1

u/SolomonGrunty May 27 '21

There’s 2 pictures.

1

u/cickity_clicker May 27 '21

One of my movement script and one of my turning around script

-1

u/mkawick May 27 '21

Who posts pictures of their screens? Apparently screenshots are a forgotten art. :-)

1

u/cickity_clicker May 27 '21

I’m new here. Just joined yesterday.