- Home /
Rotating a Gameobject In Three Axis Unity
Hi all,
I am trying to create a rotate tool in our game where the user can rotate a gameobject in three axis by clicking on one of the axis and dragging it to the desired rotation. Ideally I am looking to create a rotate tool very similar to the one in unity, but have become stuck as to the best way to achieve this. I have tried numerous rotation approaches from Quaternion.lookrotation to euler angles but none of them make the gameobject rotation correctly. If anybody has any ideas how to achieve this I would be very grateful. Please see below for how the rotation axis are on the gameobject at the moment:
Answer by anthot4 · Jun 01, 2018 at 08:02 AM
For anybody else who is in a similar situation the rotation method that is required is called "ArcBall rotation". I have found some information online which should help me achieve it, but it looks pretty advanced stuff.
Based on your setup, you probably don't need something like ArcBall. With Unity's rotation widget, you can rotate on individual axis (like yours) OR you can just drag the ball and it calculates the axis you want based on that. That's where the advances stuff comes in, I think.
Thanks your you reply, I have tried rotating the gameobject on individual axis but it doesn't rotate the right way if I rotate multiple axis. The rotation doesn't seem to take it to account the object's local rotation value.
Answer by hawksandwichgames · Jun 01, 2018 at 08:04 AM
Hey there, the way I'd go about it is by storing x,y and z rotation as separate floats then setting the object's rotation according to those values.
float rotX,rotY,rotZ;
void Something () {
//first, change your values according to how the player pulls the widget.
//now we'll give them the ability to loop around. I'm not testing this as I write it, so I'm not sure if going the full 360 is what works or not, but you get the idea.
if (rotX > 360f) rotX=0f;
if (rotX < 0f) rotX = 360f;
if (rotY > 360f) rotY=0f;
if (rotY < 0f) rotY = 360f;
if (rotZ > 360f) rotZ=0f;
if (rotZ < 0f) rotZ = 360f;
//next, let's set the object's rotation
obj.transform.rotation = Quaternion.Euler (rotX, rotY, rotZ);
}
Thanks for your answer, I have tried a similar approach to this already but havn't tried setting the rotation to how the player changes the values at the start. I'll try this and let you know if it works.
It didn't work sadly, please see below for the way I have done the code for it currently. I have an invisible plane which is perpendicular to brick for each axis. I then cast a ray from the users mouse position which hits a point on this plane which the brick then rotates towards. This works apart from the fact that the brick doesn't seem to rotate on its local axis when rotated on more than one axis.
if (Currentrotaxis == CurrentRotAxis.X) {
RaycastHit Hit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out Hit, $$anonymous$$athf.Infinity, 1 << Layer$$anonymous$$ask.NameToLayer("RotationRayCast"))) {
Vector3 difference = Hit.point - InstatBrick.transform.position;
RotBrickX = $$anonymous$$athf.Atan2 (difference.z, difference.y) * $$anonymous$$athf.Rad2Deg;
InstatBrick.transform.rotation = Quaternion.Euler (RotBrickX, RotBrickY, RotBrickZ);
Answer by Unified2000 · Jun 01, 2018 at 12:59 PM
Vector3 selectedAxis=Vector3.up;
if (Input.GetMouseButton(0))
transform.Rotate(selectedAxis*(-Input.GetAxis("Mouse X")+Input.GetAxis("Mouse Y")));
Thanks for your suggestion @Unified2000 . I'll give this a try later.