Hi Unity people and consumers.
Currently working on an controller that can grab stuff and manipulate it.
I currently have gotten stuck due to a problem with my rotation function.
I want it to be able to rotate stuff relatively to how your looking at it.
Which mean that if I drag my mouse away from me I want my object to rotate on the up axis away from me. If I drag it towards me I want it to rotate towards me. I’ve gotten it to work on the Y axis (left - right) but it just won’t work on the X axis (up - down).
Here is a picture that Illustrates which way I want it to rotate.
.
And here is my (old) rotation function:
void rotateObject() {
float RotationX = InteractingObject.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * -0.5f;
float RotationY = InteractingObject.transform.localEulerAngles.x + Input.GetAxis("Mouse Y") * -0.5f;
InteractingObject.transform.localEulerAngles = new Vector3(RotationY, RotationX, 0);
}
The cube is the ‘InteractingObject’, also using my way the rotation gets to a point where it just kinda sticks to the angle and won’t move any further. Again only happens when I want to rotate around the X axis. everything around the Y axis works.
I might just be blind to whats in-front of me so please any ideas let me know.
Here is another way I’ve tried based on an script I’ve found posted on another question:
public Quaternion rotateByAxis(float MouseX, float MouseY) {
Camera mainCam = Camera.main;
Vector3 camRelativeUp = mainCam.transform.TransformDirection(Vector3.up);
Vector3 camRelativeRight = mainCam.transform.TransformDirection(Vector3.right);
angleX += Mathf.Round(MouseX);
angleY += Mathf.Round(MouseY);
Quaternion leftRight = Quaternion.AngleAxis(angleX, camRelativeUp);
Quaternion upDown = Quaternion.AngleAxis(angleY, camRelativeRight);
Quaternion rotation = (leftRight * upDown); //Rotates fine on the Y axis no matter how its rotated in the X
//Rotates on the object local X axis. Rather than using the camera for
//some reason.
return rotation;
}
However this seems to work based on which way you decide to multiply it… if you put leftRight * upDown the left to right works and upDown is fixed to a local axis. While the other way around (upDown * leftRight) the X axis works but left to right is fixed at an local axis. It seems no matter what I do I’ll get these problems. Really hoping to see an answer on this one.
Another Update:
InteractingObject.transform.RotateAroundLocal(new Vector3(Input.GetAxis("Mouse Y"), -Input.GetAxis("Mouse X"), 0), Time.deltaTime * rotationSpeed);
//^ transform.RotateAroundLocal(Vector3 axis, float angle);
This function does what I want but in a some what jerky way, it doesn’t rotate directly towards the player view or away, so it still feels wrong but is still towards what I’m looking for. Still looking for an answer so please don’t hold back with ideas.