## How to use LookRotation to rotate an object based on hand positions? - c#

I'm creating an interaction system for a VR game and I'm struggling with two hand interactions. I'm using the Quaternion.LookRotation() method to generate the rotation of the grabbed object based on the positions and rotations of the hands. The forward part is pretty simple:
Vector3 fwd = (primaryHand.position - secondaryHand.position).normalized;
The "up" part is what I have difficulty with. Initially I tried using the average up direction of both hands:
Vector3 avgUp = Quaternion.Slerp(primaryHand.rotation, secondaryHand.rotation, 0.5f) * Vector3.up;
There is an issue with this approach: the hand's up vector might get aligned with the fwd vector, which causes the object to flip over when it goes over it. Here is a simple illustration of the problem:
The light green arrows represent the up direction of the hands, while the dark green is the calculated direction used as an argument for the LookRotation() method.
The obvious solution seems to be to pick a different fixed vector instead of "up", one which won't be so easily aligned with the fwd vector. In the example it could be a vector aligned with the fingers. But keep in mind that there are no restrictions on initial hand rotation so no matter which vector you choose the hands can always happen to be rotated so that the vectors align. And even if you pick the an optimal vector dynamically (one that is perpendicular to fwd), it's still at best 90 degrees from aligning with fwd.
To solve this I tried restricting the direction to the values which don't cause problems but this caused another issues (I had difficulties with determining which values are ok and which should be discarded). I feel like I'm doing something wrong here, is there any better solution to this problem?

You can calculate the delta rotations of the hands and apply it to the "base up" of the object (the new up if we only take into account the change in position of hands...which will of course be orthogonal to the axis of the object). Then determine the change in angle that results in that up being rotated with each hand. Average those angles out, then apply those angles with the hand-hand axis using Quaternion.AngleAxis to the "base up" from earlier. Then you have your forward and up for Quaternion.LookRotation).
Below is an example of how you can use this, including VR hand noise simulation. To see the test, create a new scene in unity and attach this to the camera and it will build the scene on play start. There is a grip/release gui that will appear in Play view. You can adjust the hand rotation in Scene view
Vector3 leftHandPosCurrent;
Vector3 rightHandPosCurrent;
Vector3 worldAxisPrev;
Quaternion leftHandRotPrev;
Quaternion leftHandRotCurrent;
Quaternion rightHandRotPrev;
Quaternion rightHandRotCurrent;
bool isGripping;
bool firstFrameGripping;
Rigidbody grippedRB;
Transform leftHand;
Transform rightHand;
Quaternion targetRot;
/*
* On subsequent frames of gripping, calculate deltas in positions and
* rotations, average out the hand's effects, then apply them to the gripped
* object
*/
void HandleGrippedRot()
{
Vector3 worldAxisCurrent = rightHandPosCurrent - leftHandPosCurrent;
if (!firstFrameGripping)
{
Vector3 prevUp = targetRot * Vector3.up;
// we haven't moved the transform based on the hands yet, so
// find the new up would be ignoring hand rotations
Vector3 newUp = Quaternion.FromToRotation(worldAxisPrev,
worldAxisCurrent) * prevUp;
float leftHandAngle = GetDegRot(newUp, leftHandRotPrev,
leftHandRotCurrent, worldAxisCurrent);
float rightHandAngle = GetDegRot(newUp, rightHandRotPrev,
rightHandRotCurrent, worldAxisCurrent);
float avgAngle = (leftHandAngle + rightHandAngle) * 0.5f;
newUp = Quaternion.AngleAxis(avgAngle, worldAxisCurrent) * prevUp;
targetRot = Quaternion.LookRotation(worldAxisCurrent,
newUp);
}
else
{
firstFrameGripping = false;
}
leftHandRotPrev = leftHandRotCurrent;
rightHandRotPrev = rightHandRotCurrent;
worldAxisPrev = worldAxisCurrent;
}
/*
* Given the "up" of the object without taking hand rotations into account
* and the axis, determine how a hand's delta rotation affects that up
* around the axis and return the angle of that rotation
*/
float GetDegRot(Vector3 baseUp, Quaternion prevHandRot, Quaternion curHandRot,
Vector3 axis)
{
Vector3 adjUp = (curHandRot * Quaternion.Inverse(prevHandRot)) * baseUp;
adjUp = Vector3.ProjectOnPlane(adjUp, axis);
return Vector3.SignedAngle(baseUp, adjUp, axis);
}
void Update()
{
AddVRNoise(leftHand);
AddVRNoise(rightHand);
leftHandPosCurrent = leftHand.position;
rightHandPosCurrent = rightHand.position;
leftHandRotCurrent = leftHand.rotation;
rightHandRotCurrent = rightHand.rotation;
if (isGripping)
{
HandleGrippedRot();
}
}
void StartGrip()
{
if (isGripping) return;
isGripping = true;
firstFrameGripping = true;
// grippedTransform is set accordingly at some point
}
void EndGrip()
{
if (!isGripping) return;
isGripping = false;
}
/*
* example of using targetRot to move rb
*/
private void FixedUpdate()
{
if (!isGripping) return;
Quaternion delta = targetRot
* Quaternion.Inverse(grippedRB.transform.rotation);
delta.ToAngleAxis(out float angle, out Vector3 axis);
// convert to shortest angle form
if (angle > 180f)
{
axis = -axis; angle = 360f - angle;
}
grippedRB.angularVelocity = angle * 0.25f * axis;
}
/*
* just for testing purposes
*/
void Start()
{
leftHand = CreateHand(true);
leftHand.position = Vector3.left;
rightHand = CreateHand(false);
rightHand.position = Vector3.right;
CreateArrow();
}
/*
* just for testing purposes
*/
void AddVRNoise(Transform hand)
{
Quaternion noise = Random.rotation;
noise.ToAngleAxis(out float angle, out Vector3 axis);
angle = 100f * Time.deltaTime;
noise = Quaternion.AngleAxis(angle, axis);
Quaternion noisyRot = hand.rotation * noise;
hand.rotation = noisyRot;
}
/*
* just for testing purposes
*/
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 50), "Grip"))
{
StartGrip();
}
if (GUI.Button(new Rect(100, 0, 100, 50), "Release"))
{
EndGrip();
}
}
/*
* just for testing purposes
*/
Transform CreateHand(bool isLeft)
{
string handName = isLeft ? "Left" : "Right";
GameObject hand = new GameObject($"{handName}hand");
GameObject palm = GameObject.CreatePrimitive(PrimitiveType.Cube);
palm.transform.localScale = new Vector3(0.5f, 0.2f, 1f);
palm.transform.SetParent(hand.transform);
GameObject thumb = GameObject.CreatePrimitive(PrimitiveType.Cube);
thumb.transform.localScale = new Vector3(0.2f, 0.1f, 0.1f);
thumb.transform.SetParent(hand.transform);
thumb.transform.localPosition = new Vector3(isLeft ? 0.32f : -0.32f,
0f, -.31f);
return hand.transform;
}
/*
* just for testing purposes
*/
void CreateArrow()
{
GameObject arrow = new GameObject();
GameObject body = GameObject.CreatePrimitive(PrimitiveType.Cube);
body.transform.localScale = new Vector3(1f, 1f, 5f);
body.transform.SetParent(arrow.transform);
GameObject head = GameObject.CreatePrimitive(PrimitiveType.Cube);
head.transform.SetParent(arrow.transform);
head.transform.localEulerAngles = Vector3.up * 45f;
head.transform.localPosition = new Vector3(0f, 0f, 2.5f);
grippedRB = arrow.AddComponent<Rigidbody>();
grippedRB.useGravity = false;
arrow.transform.position = 2f * Vector3.up;
}

## Related

### Rotating raycast2D back and forth with object

I made a circle and attach a lazer box on top of it. The lazer will fire a raycast to its upper y axis (straight up). I also add a line renderer to view it. I want the raycast to rotate 90 degrees back and forth. Sort of like its scanning everything on top. My problem is that its not working properly. It does rotate back and forth but If I move the x position of the lazer object, the raycast will rotate in a weird angle. Script for lazer object public LineRenderer lineRenderer; public LayerMask layerMask; public float laserSpeed; Vector3 pointA; Vector3 pointB; Vector3 castPosition; RaycastHit2D rayCast; float time; void Start() { pointA = transform.eulerAngles + new Vector3(0f, 0f, 90f); pointB = transform.eulerAngles + new Vector3(0f, 0f, -90f); } void Update() { time = Mathf.PingPong(Time.time * laserSpeed, 1); transform.eulerAngles = Vector3.Lerp(pointA, pointB, time); castPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z); rayCast = Physics2D.Raycast(castPosition, transform.TransformDirection(Vector2.up), 10f, layerMask); lineRenderer.SetPosition(0, castPosition); lineRenderer.SetPosition(1, transform.TransformDirection(Vector2.up) * 10f); }

Using eulerAngles for continuous animations is quite "dangerous". Unity stores the rotations as Quaternion and there are multiple ways of how to represent these in euler space! When you read the .eulerAngles property, Unity converts the Quaternion's internal representation of the rotation to Euler angles. Because, there is more than one way to represent any given rotation using Euler angles, the values you read back out may be quite different from the values you assigned. This can cause confusion if you are trying to gradually increment the values to produce animation. To avoid these kinds of problems, the recommended way to work with rotations is to avoid relying on consistent results when reading .eulerAngles particularly when attempting to gradually increment a rotation to produce animation. For better ways to achieve this, see the Quaternion * operator. so you should rather go for Quaternion and do e.g. And then you are using transform.TransformDirection(Vector2.up) which is a direction and pass it to your line renderer as a position. What you want there is rather the position combined from transform.position + transform.up So together it should probably rather be public LineRenderer lineRenderer; public LayerMask layerMask; public float laserSpeed; private Quaternion originalRotation; private Quaternion minRotation; private Quaternion maxRotation; void Start() { originalRotation = transform.rotation; minRotation = originalRotation * Quaternion.Euler(0, 0, -90); maxRotation = originalRotation * Quaternion.Euler(0, 0, 90); } void Update() { // Note that Vector3 is a "struct" -> there is no need to manually use "new Vector3(transform.position.x, ...)" var startPosition = transform.position; lineRenderer.SetPosition(0, startPosition); var factor = Mathf.PingPong(Time.time * laserSpeed, 1); // instead of the eulers rather use Quaternion transform.rotation = Quaternion.Lerp(minRotation, maxRotation, factor); // "transform.up" basically equals using "transform.TransformDirection(Vector3.up)" var rayCast = Physics2D.Raycast(startPosition, transform.up, 10f, layerMask); if(rayCast.collider) { // when you hit something actually use this hit position as the end point for the line lineRenderer.SetPosition(1, rayCast.point); } else { // otherwise from the start position go 10 units in the up direction of your rotated object lineRenderer.SetPosition(1, startPosition + transform.up * 10f); } }

### When I make an object face a mouse cursor in Unity it eventually offsets

I made a script that makes the player point towards the mouse cursor, but recently I discovered a bug. When I move the mouse cursor too much (An example being when I spin the mouse around the object in circles, causing the object to move around.), the object ends up pointing a bit off of where the mouse should be. As in, the cursor would signal the object to look at it, and the object ends up looking the slightest bit off, making it feel quite odd after maneuvering quickly. How can I make it so the object always faces the cursor, with no offsets, even when I move the cursor as much as possible. private void LateUpdate() { Vector3 lookAtPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 mousePoint = new Vector3(lookAtPoint.x, lookAtPoint.y, 0); float angle = getAngle(transform.position, mousePoint); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), 9f * Time.deltaTime); float getAngle(Vector3 currentLocation, Vector3 mouseLocation) { float x = mouseLocation.x - currentLocation.x; float y = mouseLocation.y - currentLocation.y; return angle = Mathf.Rad2Deg * Mathf.Atan2(y, x); } }

Looks like it's down to the way that you are using Quaternion.Lerp(). In particular, the last parameter - it is meant to be a value between 0f and 1f which you supply, it does not auto-increment for you. So to fix this issue, what you want to do is save off a float somewhere. When you move the mouse (mouse position has changed since last frame) then start that value at 0f again. Then increment it at some value every frame until it is equal to or greater than 1f. Doing this will not only fix your bug. It will, depending on how fast your increment, give you a smooth rotation effect. Below is an example. internal class SomeClass : MonoBehaviour { private float lerpAmount = 0f; private Vector3 cachedMousePosition = Vector3.zero; private void LateUpdate() { var mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) * new Vector3(1, 1, 0); bool recalculateRotation = false; if (this.cachedMousePosition != mousePosition) { this.lerpAmount = 0f; recalculateRotation = true; } if (this.lerpAmount < 1f) { this.lerpAmount = Mathf.Min(this.lerpAmount + Time.deltaTime, 1f); recalculateRotation = true; } if (recalculateRotation) { float angle = getAngle(transform.position, mousePoint); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), this.lerpAmount); } float getAngle(Vector3 currentLocation, Vector3 mouseLocation) { float x = mouseLocation.x - currentLocation.x; float y = mouseLocation.y - currentLocation.y; return angle = Mathf.Rad2Deg * Mathf.Atan2(y, x); } }

### Making Field of view using Raycasts With for loops (Using Unity C#)

Code: void IdleState () { RaycastHit hit; for (float i = -ViewWidth; i < ViewWidth; i++) { float Iterater = i/20; if (Physics.Raycast(transform.position, transform.forward + new Vector3(Iterater,0,0), out hit, ViewRange)) { Debug.DrawRay(transform.position,(transform.forward + new Vector3(Iterater,0,0)).normalized * ViewRange, Color.red); if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Player")) { FoundPlayer(hit.transform); } } } } Problem: In Unity I am trying to create a Field of view for an enemy AI by drawing multiple raycasts with for loops. For some reason the raycasts do this: GIF I've been trying to fix this for days Please help! Also the FoundPlayer() function in case you need it: void FoundPlayer (Transform DetectedObject) { float step = TurnSpeed * Time.deltaTime; Vector3 Direc = DetectedObject.position - transform.position; Vector3 RotMath = Vector3.RotateTowards(transform.forward, Direc,step,0.0f); transform.rotation = Quaternion.LookRotation(RotMath); Vector3 CurrentRot = transform.eulerAngles; transform.rotation = Quaternion.Euler(0,CurrentRot.y,0); } The FoundPlayer() function just rotates the enemy towards the player when one of the raycasts hit it.

Regarding Raycast: transform.Forward is in world-space coordinates, to which you add some fraction of world-space X-axis offset. What you wanted is to add some fraction of 'local-space' X-axis. transform.Right is the world-space conversion of local-space X-axis. Try: var rayDirection = (transform.Forward + (Vector3.Scale(transform.Right, new Vector3(Iterater, 0, 0))).normalized; Regarding Fields of View: If all you want to do is check what objects are within a FOV, start finding everything within a sphere, then filter those objects to what's within appropriate angles from transform.forward: float ViewRange = 10; float hHalfFov = 45; // Horizontal Half-Field of View (Degrees) float vHalfFov = 45; // Vertical Half-Field of View (Degrees) void IdleState() { // Find all colliders within ViewRange var hits = Physics.OverlapSphere(transform.position, ViewRange); foreach (var hit in hits) { if ( hit.gameObject == this.gameObject ) continue; // don't hit self // Check FOV var direction = (transform.position - hit.transform.position); var hDirn = Vector3.ProjectOnPlane(direction, transform.up).normalized; // Project onto transform-relative XY plane to check hFov var vDirn = Vector3.ProjectOnPlane(direction, transform.right).normalized; // Project onto transform-relative YZ plane to check vFov var hOffset = Vector3.Dot(hDirn, transform.forward) * Mathf.Rad2Deg; // Calculate horizontal angular offset in Degrees var vOffset = Vector3.Dot(vDirn, transform.forward) * Mathf.Rad2Deg; // Calculate vertical angular offset in Degrees if (hOffset > hHalfFov || vOffset > vHalfFov) continue; // Outside of FOV Debug.DrawLine(transform.position, hit.transform.position, Color.red); if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Player")) { FoundPlayer(hit.transform); } } }

### How do I make a 2D object face the direction it is moving like drifting

As shown in the picture above, I have two GameObjects: the car and the circle. The car follows the circle, and the circle is moving with the cursor. Currently, my car is following the circle from a distance. When the circle moves along the x-axis, I want to rotate the car like it's drifting. Here is my car follow script: public class Follow : MonoBehaviour { public Transform leader; public float followSharpness = 0.1f; Vector3 _followOffset; void Start() { // Cache the initial offset at time of load/spawn: _followOffset = transform.position - leader.position; } void LateUpdate() { // Apply that offset to get a target position. Vector3 targetPosition = leader.position + _followOffset; //GetComponent<Rigidbody2D>().rotation = 1.5f; // Keep our y position unchanged. //targetPosition.y = transform.position.y; // Smooth follow. transform.position += (targetPosition - transform.position) * followSharpness; } }

You could try to use Transform.LookAt: void LateUpdate() { Vector3 targetPosition = leader.position + _followOffset; transform.position += (targetPosition - transform.position) * followSharpness; transform.LookAt(leader); } Disclaimer: I'm not able to test that this code works right now, you'll have to try it out and see if it produces the desired result.

So from what you described, is you want to make the car always facing the cursor, so it is always "looking at" the cursor, here is how you can do it: Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; diff.Normalize(); float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90); From Answer: LookAt 2D Equivalent ?

### Unity3D: Find Raycasthit direction

I'm trying to make a Billiard game and I wanna calculate the direction on which the Cue Ball(white ball) will be moving after it hits another ball. As you can see I wanna calculate the angle/direction in which the RAY hits the ball and the angle/direction on which the raycast will change its direction into. I need the angle to display as a Vector3 variable so I can use it on the linerenderer(3). I already calculated the direction that the ball that gets hit will go. If you could help me on this that would be great! Current code: RaycastHit hitz; if (Physics.SphereCast(transform.position, 0.8f, location - transform.position, out hitz, Mathf.Infinity, lmm2)) { lineRenderer2 = hitz.collider.GetComponentInChildren<LineRenderer>(); lineRenderer2.SetVertexCount(2); if (!Input.GetKey(KeyCode.Mouse0)) lineRenderer2.SetPosition(0, hitz.point); if (!Input.GetKey(KeyCode.Mouse0)) { Vector3 start = hitz.point; Vector3 end = start + (-hitz.normal * 4); if (lineRenderer2) { if (!Input.GetKey(KeyCode.Mouse0)) lineRenderer2.SetPosition(1, end); } if(lineRenderer3) { anglelel = Vector3.Angle(hitz.normal, hitz.point); Vector3 cross = Vector3.Cross(hitz.normal, hitz.point); if(cross.y > 0) { tzt = Quaternion.AngleAxis(90f, hitz.normal) *realStick.transform.forward; } if (cross.y < 0) { anglelel = -anglelel; tzt = Quaternion.AngleAxis(270f, hitz.normal) * realStick.transform.forward; } Vector3 start2 = hitz.point; Vector3 end2 = start2 + ((tzt) * 5f); lineRenderer3.SetPosition(0, hitz.point); lineRenderer3.SetPosition(1, end2); } } } Thank you for your time. Edit: This part of the code has been changed to this one, currently makign some progress but still, it's not good enough. Before if(lineRenderer3) { Vector3 start2 = hitz.point; //THIS IS WHERE I'M CURRENTLY STUCK AT Vector3 end2 = start2 + (hitz.point * 0.7f); lineRenderer3.SetPosition(0, hitz.point); lineRenderer3.SetPosition(1, end2); } After if(lineRenderer3) { anglelel = Vector3.Angle(hitz.normal, hitz.point); Vector3 cross = Vector3.Cross(hitz.normal, hitz.point); if(cross.y > 0) { tzt = Quaternion.AngleAxis(90f, hitz.normal) *realStick.transform.forward; } if (cross.y < 0) { anglelel = -anglelel; tzt = Quaternion.AngleAxis(270f, hitz.normal) * realStick.transform.forward; } Vector3 start2 = hitz.point; Vector3 end2 = start2 + ((tzt) * 5f); lineRenderer3.SetPosition(0, hitz.point); lineRenderer3.SetPosition(1, end2); }

Let's take this piece by piece. First off, this is a classic Physics 101 problem. The angle 2 billiard balls make on impact is a perfect 90 degree angle. Note how the green and blue vector in the following picture make a right angle: Now, you should also notice that from the point of contact to the center of both balls is normal to the surface of both balls. This means that in unity, we can use the hit.normal to get the direction of travel for the ball we hit. We just need to invert it by doing: -1 * hit.normal Now, to get the direction the cue ball travels, we just need to rotate the previous vector 90 degrees. We can do this with a quaternion. We create a 90degree rotation about the up direction (or whatever direction is normal to the pool table) by doing: Quaternion.AngleAxis(-90, Vector3.up) We can then calculate the angle between the original vector of travel and the angle the cue ball will travel at by doing Vector3.Angle(-1 * cue.up, rotate90 * hit.normal) Let's look at this visual example from my test scene: I color coded the vectors in unity to match the diagram above. The only difference you may notice is the black vector, which represents our hit.normal. Here's the code: public class Main : MonoBehaviour { public Transform cue,cueBallPostHit; public int dist = 10; public Color red,green,blue; RaycastHit hit; float scale,ballAngle; Quaternion rotate90; Vector3 cueBallHitPosition; void Start () { rotate90 = Quaternion.AngleAxis(-90, Vector3.up); } void FixedUpdate () { if(Physics.SphereCast(cue.position, .5f, cue.up, out hit, dist)) { // Calculate variables cueBallHitPosition = hit.point + (.5f * hit.normal); scale = (cue.position - hit.point).magnitude; ballAngle = Vector3.Angle(-1 * cue.up, rotate90 * hit.normal); print(ballAngle); // Cue Ball Direction and normal Debug.DrawLine(cue.position, cueBallHitPosition, red); Debug.DrawRay(cueBallHitPosition, hit.normal, Color.black); // Ball direction Debug.DrawRay(hit.point + (-.5f * hit.normal), -1 * hit.normal * scale, blue); // Cue Ball Direction Debug.DrawRay(cueBallHitPosition, rotate90 * hit.normal * scale, green); // Visual for where the ball will hit cueBallPostHit.position = cueBallHitPosition; } else { Debug.DrawRay(cue.position, cue.up * dist, blue); cueBallPostHit.position = cue.position + (2 * cue.up); } } } Hopefully that should be enough to help you get started in the right direction, but if you have questions, let me know and I'll add some more explanations.