Quaternion.FromToRotation to calculate Rotation between two objects
I'm trying to use Quaternion.FromToRotation function to calculate the exact difference in Euler angle between two objects.
I converted the Quaternion by multiplying the simple direction vector to be used as a parameter. (http://answers.unity3d.com/questions/525952/how-i-can-converting-a-quaternion-to-a-direction-v.html)
But I couldn't get exact angle difference.
Here is code and screenshot.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestQuaternionFromToRotation : MonoBehaviour
{
public Transform fromTransform;
public Transform toTransform;
public Vector3 fromVector;
public Vector3 toVector;
void Update()
{
fromVector = fromTransform.rotation * Vector3.up;
toVector = toTransform.rotation * Vector3.up;
transform.rotation = Quaternion.FromToRotation(fromVector, toVector);
}
}
I expected (20, 40, 60), of course, but the results were different.
What's wrong with this script? Is it related to Gimbal Lock?
Answer by Cheerbtms · Nov 24, 2017 at 10:56 AM
I generally use FromToRotation when I want a specific part of an object to face a specific direction. For example, if I've got a robot arm facing some direction, and there's a hand attached to it at some arbitrary point, and I want to position the hand to pick up a box, I would calculate the rotation like this:
Vector3 currentOffset = arm.transform.InverseTransformPoint(hand.transform.position);
Vector3 desiredOffset = arm.transform.InverseTransformPoint(box.transform.position);
arm.transform.localRotation *= Quaternion.FromToRotation(currentOffset, desiredOffset);
Hope that helps someone!
Your answer
Follow this Question
Related Questions
Rotate around a point with a specific angle 0 Answers
More problems with arrow shooting 0 Answers
Setting a quaternion's rotation value 0 Answers
Rotating camera not working? 0 Answers
How to smoothly rotate the object? 2 Answers