Rotate GameObject on specific axis
Hello together
I'm a Unity beginner, so my question is may easy to answer to you. But I'm struggling now for a while and don't know what to do else.
I want to implement a "Holographic Puzzle" for the Windows HoloLens. For that I want to rotate my puzzle Parts. Simplified my puzzle parts are just cubes. The user target these puzzle parts with "Gaze", a ray coming out of the users head in the direction the user is looking. If the ray hits an object (puzzle part), I draw an indicator to show that the user can rotate this object. This Indicator is on transformed to the plane which is hit. See Image below. For that I transform the indicator:
// Assign gameObject's transform up vector equals GazeManager's instance Normal.
gameObject.transform.up = GazeManager.Instance.Normal;
Now my problem: I want to rotate the GameObject if the user make a Gesture. This rotation should be around this GazeManager's instance Normal AND the rotation should be arround the center of the GameObject. See Image below.
I tried a lot and searched a lot, but nothing worked. I paste my actual code below. If someone can help me it would be very friendly
Thanks a lot, Lukas
Code:
public class GestureAction : MonoBehaviour
{
....
private Vector3 startUpdateNormalPosition = Vector3.zero;
private Vector3 startUpdateGazeOriginPosition = Vector3.zero;
....
void PerformNavigationStart(Vector3 position)
{
Debug.Log("PerformNavigationStart");
startUpdateNormalPosition = GazeManager.Instance.Normal;
startUpdateGazeOriginPosition = GazeManager.Instance.gazeOrigin;
}
void PerformNavigationUpdate(Vector3 position)
{
// Debug.Log("PerformManipulationUpdate");
// rotationFactor = GestureManager.Instance.NavigationPosition.z * RotationSensitivity;
Vector3 navigationDirection = AppContext.Instance.AppState.NavigationStartPosition - position;
float rotationMaximum = Mathf.Max(navigationDirection.x, navigationDirection.y, navigationDirection.z);
Debug.Log("rotationMaximum: " + rotationMaximum);
// Apply rotation to object
//transform.Rotate(new Vector3(rotationFactor_X, -1 * rotationFactor_Y, rotationFactor_Z));
// transform.Rotate(new Vector3(0, -1 * rotationFactor, 0));
// transform.Rotate(rotationVector, rotationSpeed);
// Vector3 worldRotation = this.transform.TransformDirection(AppContext.Instance.AppState.NavigationStartRotationAxis);
Vector3 worldRotation = this.transform.TransformDirection(AppContext.Instance.AppState.NavigationStartRotationAxis);
// transform.Rotate(worldRotation, rotationSpeed);
transform.RotateAround(startUpdateGazeOriginPosition, startUpdateNormalPosition, rotationMaximum*20*Time.time);
}
}
Answer by lukas_werz · Nov 04, 2016 at 08:34 AM
Sometimes I ask my questions just 5 minutes to early. I was really close. The correct rotation is:
void PerformNavigationUpdate(Vector3 position)
{
Vector3 navigationDirection = AppContext.Instance.AppState.NavigationStartPosition - position;
float rotationMaximum = Mathf.Max(navigationDirection.x, navigationDirection.y, navigationDirection.z);
Debug.Log("rotationMaximum: " + rotationMaximum);
// Apply rotation to object
transform.RotateAround(transform.position, startUpdateNormalPosition, rotationMaximum * Time.time);
}
Wouldn't just rotating on the local rotation using
transform.rotation.localRotation = new Quaternion.Euler(xRotation, yRotation, zRotation);
be enough?
Your answer
Follow this Question
Related Questions
ROTATION STOPS when i move the char on the air! 0 Answers
How to only slerp Y and Z, whilst independently rotating X 0 Answers
Make GameObject rotate around another around random axis but with fixed distance 0 Answers
Mixing transform.rotation and transform.rotate? 1 Answer
How to object rotate on a single axis towards another object? C# 2 Answers