Public Group

# [Unity] Rotating a 3D character in 2D space

This topic is 1018 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I been struggling with this issue for 5 days now.

Searched the answers and still nothing concrete.

All I want is for my 3d character(Which is a ball) to look left and right.

But the rotation inputs get messed up somehow, making my character turn in weird unexpected ways and it's like I cant edit the rotation values to just go back and forth between two Y values. below is my code.

 void Start()
{

rb2d = gameObject.GetComponent<Rigidbody2D>();

lookRight = transform.rotation;
lookLeft = lookRight * Quaternion.Euler(0, 180, 0);

}

void FixedUpdate()
{

float move = Input.GetAxis("Horizontal");

rb2d.velocity = new Vector2(move * maxSpeed, rb2d.velocity.y);

if (move > 0 && !facingRight)
{

facingRight = !facingRight;
transform.rotation = lookRight;

Debug.Log("Transform: "+transform.localRotation.ToString());
}
else if (move < 0 && facingRight)
{

facingRight = !facingRight;
transform.rotation = lookLeft;

Debug.Log("Transform: "+transform.localRotation.ToString());

}

}


I really need some help here, because I feel this should not be so difficult to figure out.

##### Share on other sites

You are multiplying your look right rotation value by 180 in the Y axis. You should be adding it shouldn't you?

##### Share on other sites

You are multiplying your look right rotation value by 180 in the Y axis. You should be adding it shouldn't you?

The Add operator isn't allowed with Quaternions. Is their another type I could use?

##### Share on other sites

Don't worry, you're using Quaternion correctly.

But the rotation inputs get messed up somehow, making my character turn in weird unexpected ways and it's like I cant edit the rotation values to just go back and forth between two Y values. below is my code.

Is the problem with movement, or rotation?

Your code should make the character alternate be rotated in two different directions depending on your input axis. What is actually happening?

As for your movement, it looks like you might be mixing 2d physics and 3d objects in the wrong way.

It looks like your y-axis is up (based on your left/right rotation), meaning your character travels on the (x, z) plane. But Unity 2D physics operates on a Vector2, so (x, y). I'm not familiar enough with Unity 2d physics to know if it supports mapping it to arbitrary 2d planes. But for instance, I'm not sure what groundCheck is, but if this is a Transform, then the following:

is silently converting the (x, y) of a Vector3 to a Vector2. I doubt that's what you want.

You might need to re-orient everything so that the z axis points upward in your world.
Edited by phil_t

##### Share on other sites

One of my big pet peeves with Unity is it's silent conversion of Vector3s to Vector2s.  I get bit by it all the time.

That said, I'm not sure that's the issue, have you tried moving to a rotation around the Z axis?  I believe the default for Unity is that the Z axis is pointing towards the screen. (or is it away?  either way, rotating by the Z axis will reveal the issue)

##### Share on other sites

The problem is with rotation, I want my character to look left and right, but instead it seems to rotate in different angles every time I press left or right. Like sometimes it rotates 180 degrees other times it rotates  90 degrees other times it looks like 45 degrees, very inconsistent in the angles it rotates by.

I use grounded to determine if the character is on the ground to determine if it can JUMP.  I have it set up that everything except the character is a ground layer.

   void Start()
{

rb2d = gameObject.GetComponent<Rigidbody2D>();
//anim = GetComponent<Animator>();

lookRight = transform.localRotation;
lookLeft = lookRight * Quaternion.Euler(0, 0, 180);

}


I tried editing the Z axis but instead of turning left and right in all different crazy angles, it went up and down in all different crazy angles.

##### Share on other sites

Don't worry, you're using Quaternion correctly.

But the rotation inputs get messed up somehow, making my character turn in weird unexpected ways and it's like I cant edit the rotation values to just go back and forth between two Y values. below is my code.

Is the problem with movement, or rotation?

Your code should make the character alternate be rotated in two different directions depending on your input axis. What is actually happening?

As for your movement, it looks like you might be mixing 2d physics and 3d objects in the wrong way.

It looks like your y-axis is up (based on your left/right rotation), meaning your character travels on the (x, z) plane. But Unity 2D physics operates on a Vector2, so (x, y). I'm not familiar enough with Unity 2d physics to know if it supports mapping it to arbitrary 2d planes. But for instance, I'm not sure what groundCheck is, but if this is a Transform, then the following:

is silently converting the (x, y) of a Vector3 to a Vector2. I doubt that's what you want.

You might need to re-orient everything so that the z axis points upward in your world.

One of my big pet peeves with Unity is it's silent conversion of Vector3s to Vector2s.  I get bit by it all the time.

That said, I'm not sure that's the issue, have you tried moving to a rotation around the Z axis?  I believe the default for Unity is that the Z axis is pointing towards the screen. (or is it away?  either way, rotating by the Z axis will reveal the issue)

The problem is with rotation, I want my character to look left and right, but instead it seems to rotate in different angles every time I press left or right. Like sometimes it rotates 180 degrees other times it rotates  90 degrees other times it looks like 45 degrees, very inconsistent in the angles it rotates by.

I use grounded to determine if the character is on the ground to determine if it can JUMP.  I have it set up that everything except the character is a ground layer.

void Start()
{

rb2d = gameObject.GetComponent<Rigidbody2D>();
//anim = GetComponent<Animator>();

lookRight = transform.localRotation;
lookLeft = lookRight * Quaternion.Euler(0, 0, 180);

}

I tried editing the Z axis but instead of turning left and right in all different crazy angles, it went up and down in all different crazy angles.

##### Share on other sites

Then maybe the physics simulation is interfering with your setting of rotation. Do you have "freezeRotation" set?

http://docs.unity3d.com/ScriptReference/Rigidbody2D-freezeRotation.html

Yeah I do, I froze the Z axis, but I didnt use a variable I just clicked it in rigidbody2d constraints "freeze Rotation Z", Also when I say different angles, I mean different angles all along the Y axis.

Edited by Craigis

##### Share on other sites

Try getting rid of all the rigidbody code and the rigidbody component as a test.   Maybe it's colliding with something and causing it to turn?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 27
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634100
• Total Posts
3015527
×