- Home /
how to convert rotation of an object in degrees X
I need only rotate the x-axis of an object to the mouse or touch, I'm currently using this code to rotate the object:
Vector3 screenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y);
screenPoint.z = -transform.position.z; //distance of the plane from the camera
dragObject.parent.LookAt(Camera.main.ScreenToWorldPoint(screenPoint));
But you change x, y, z axes, I want to change only x-axis, or convert the x, y, z in a single angle x. So with y and z to 0 degrees
Does anyone have the same problem and managed to solve it? thanks in advance
It doesn't make sense to me that you are taking a screen pixel coordinate screenPoint
and then setting its .z parameter to a world coordinate -transform.position.z
and then converting the whole vector to world coordinates.
Is your camera looking along the Z-axis ?
You say you want to rotate around X-axis so is this code meant to make an object "look back at you" from the game world (facing towards the screen and looking up and down at the dragged object) ? Please explain more (screenshot?)
I set the z coordinate, because this script is attached to the camera and not the object to rotate (that is dragObject), by setting the -transform.position.z raycast that part from the screen stops exactly at (x, y, 0 ), in this way the object rotates along the z axis, otherwise the object always point towards the camera.
I care rotate an object towards the mouse or touch by changing only a value ranging from 0 to 360 in degrees (I wrote x axis as an example and figure out how to fix it).
With the lines of code that I put in the post upper changed all 3 values of rotation.
I tried to use:
transform.Rotate
transform.eulerAngles
Quaternion.Euler
the object rotates, but is not facing the mouse.
Answer by Ardito · Mar 13, 2015 at 03:59 PM
I resolved, I put the code I used and I hope to help someone in the future.
float ris = 0;
if(Object.eulerAngles.y == 270)
{
if(Object.eulerAngles.x <= 90 && Object.eulerAngles.x >= 0) ris = Object.eulerAngles.x + 180;
else ris = Object.eulerAngles.x - 180;
}
else if(dragObject.parent.eulerAngles.y == 90)
ris = -(dragObject.parent.eulerAngles.x - 360);
Debug.Log((int)ris);
Answer by m4c0 · Mar 13, 2015 at 03:10 PM
Assuming it's a 3D world, why aren't you raycasting from the click position to an invisible plane and make it look at that point? This way, the plane defines the rotation.
Is a 3D world, I have already tried to do this, but with poor results, you may kindly give me a practical example of scripting?
Your answer
Follow this Question
Related Questions
How to rotate Character depending on the inclination of the platform or its parts? 1 Answer
0-360 Y degree from Vector3.Angle 1 Answer
90 degrees isnt? 2 Answers
Rotating A Character 180 Degress 1 Answer
Limit Object Angle 2 Answers