yAxis rotation parent object.

Hi,

I have an parent object which contains the player object and a object the player looks to be pushing in front of him, together they make a rectangle shape. The parent object transform is placed in the middle of the back of the player. It’s a 3d platformer so I’m getting floats (zAxis/ xAxis) from the horizontal en vertical axis, then put those in a vector3 and add those * playerSpeed to the current position. The movement works fine, my problem is dealing with rotation. I would like the parent object to rotate on the Y axis based on the floats used for movement. It’s for a mobile game so I’m using crossplatform input.

This line “transform.Rotate (0, (zAxis - xAxis), 0)” in the script that’s on the parent object works but it keeps rotating after I’m in the desired rotation because it keeps adding on.

Is it possible to get a direction variable based of the current horizontal/vertical axis and use that to rotate the parent object and stop rotating it when it’s there and start rotating when the input changes? I’ve been trying to wrap my head around it but I’m not getting it. Any help would be very much appreciated.

Without seeing code, all I can tell you is that Mathf.Atan2() will give you a heading (in radians, not degrees!) from two cartesian coordinates, such as X,Z.

You can multiply the output by Mathf.Rad2Deg to get degrees.

If you post relevant code, be sure to use code tags. See the first post in the forum.

1 Like

Hi, and thank you for your reply. I’ll add the relevant parts of the code I have now and clarify some more.

//getting the input from the mobile controls, I’m using the single stick prefab from the standard assets

 xAxis = CrossPlatformInputManager.GetAxis ("Horizontal");
  zAxis = CrossPlatformInputManager.GetAxis ("Vertical");


//to rotate
 transform.Rotate (0, (zAxis - xAxis), 0);

//to move
 transform.position += Vector3.forward * currentSpeed * zAxis * Time.deltaTime;
 transform.position += Vector3.right * currentSpeed * xAxis * Time.deltaTime;

This is all in the update function.

This way the parent object rotates but as I said it keeps rotating because it keeps adding on. I’ll try using your code as soon as I have the time.

Line 10 is your problem. Subtracting one axis from another does not give an angle. In fact, it is mathematically meaningless.

Basic trigonometry tells us that the tangent of an angle is the opposite side over the adjacent. Remember TANOA from algebra in school?

The function I suggested is called Mathf.Atan2() for a reason: it takes 2 coordinates and returns you the arctangent, which is the inverse of the tangent. In other words, it maps from two orthogonal axis to an angle.

This means that the expression Mathf.Atan2(xAxis,zAxis) * Mathf.Rad2Deg will give you an angle based on the relative magnitude and direction of the two inputs.

You could then use that angle as your Rotate, perhaps with a 90-degree or 180-degree or other offset/negation.

Generally when you HAVE an angle, you don’t use .Rotate(), which is a relative command.

Instead when you have the angle, you just set the angle directly with a construct like this:

transform.rotation = Quaternion.Euler( 0, angleThatICalculatedAbove, 0);
2 Likes

Works like a charm, thank you!

1 Like