- Home /
How to make an object "LookAt" a target rotating around, but keep its current orientation
Hello guys !
I am working on a VR project and i want to use LookAt so that i can rotate objects intuitively. I've tried multiple ways and the best one is with LookAt.
The issue is that when i start rotating the object "snaps" to the new target.
I am rotating the object on only the Y axis and that is my goal.
How can i keep its Y axis orientation and still make it "LookAt" (not from its forward) at the target and follow it ?
Example of problem :
https://drive.google.com/file/d/1Pd3rt78RzsnU_Cq3jM-nGM9Bw2y-yExe/view?usp=sharing
here is the current code ( i use the ray-cast hit point on the ground as a target point)
RaycastHit rotatingHit;
Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayerMask);
Vector3 targetPosition = new Vector3(rotatingHit.point.x ,
ObjectToEdit.transform.position.y,
rotatingHit.point.z);
ObjectToEdit.transform.LookAt(targetPosition);
Answer by Vladislav-Videnov · May 28, 2019 at 09:38 AM
I figured it out. My problem was that i didnt understand both points are changing with rotation ( targetRotation and the objectRotation)
So what i did is :
OnGrab -> Store ObjectInitialRot and TargetRotationInitial
initialObjectRot = ObjectToEdit.transform.rotation;
RaycastHit rotatingHit;
Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayerMask);
Vector3 targetPos = rotatingHit.point - ObjectToEdit.transform.position;
targetPos.y = 0;
initialNewRot = Quaternion.LookRotation(targetPos, Vector3.up);
OnUpdate ->
RaycastHit rotatingHit;
Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayerMask);
Vector3 targetPos = rotatingHit.point - ObjectToEdit.transform.position;
targetPos.y = 0;
Quaternion delta = Quaternion.Inverse(initialNewRot) * Quaternion.LookRotation(targetPos, Vector3.up);
ObjectToEdit.transform.rotation = initialObjectRot * delta;
Result:
https://drive.google.com/file/d/18483_FOKwVc6g3IB0HejFVTyAKhbFXSN/view?usp=sharing @highpockets
Answer by highpockets · May 28, 2019 at 08:51 AM
This should work, but it is untested, let me know if something isn't right. When the mouse button gets pressed, we get the angle difference and then we rotate to the target and multiply the rotation on the y axis by the difference.
float angleDif;
if (Input.GetMouseButtonDown(0))
{
angleDif = Vector3.SignedAngle(ObjectToEdit.transform.forward, targetPosition - ObjectToEdit.transform.position);// this gets the angle difference the moment the mouse clicks on the position
}
RaycastHit rotatingHit;
Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayerMask);
Vector3 targetPosition = new Vector3(rotatingHit.point.x ,
rotatingHit.point.y,
rotatingHit.point.z);
ObjectToEdit.transform.rotation = Quaternion.Euler(ObjectToEdit.transform.rotation.eulerAngles.x, ObjectToEdit.transform.rotation.eulerAngles.y + angleDif, ObjectToEdit.transform.rotation.eulerAngles.z ) * Quaternion.LookRotation(targetPosition - ObjectToEdit.transform.position, ObjectToEdit.transform.Up);
Sorry, maybe i didnt write it clear enough.
I want the object to keep its initial Rotation ( so i do not want it to smooth / snap to the look at position )
So what do you need to change? if nothing rotates, why are you using LookAt()?
Link to a gif showing what the problem is.
I want the object not to snap, but stay at the rotation it is currently, and rotate based on where i move the mouse like shown on the gif.
I've just tried some other approach.
RaycastHit rotatingHit;
Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayer$$anonymous$$ask);
Vector3 targetPos = rotatingHit.point - ObjectToEdit.transform.position;
targetPos.y = 0;
Quaternion rotation = Quaternion.LookRotation(targetPos, Vector3.up);
ObjectToEdit.transform.rotation = initialObjectRot * rotation;
But that doesnt keep the initial rotation for some reason :/ (initialObjectRot is captured at the start of rotation )
Your answer
Follow this Question
Related Questions
Having a plane at exactly the far clip of a camera 1 Answer
How to rotate towards a target but only on the y axis ? 1 Answer
How to make AI look at player 0 Answers
Lock rotation axis? 4 Answers
LookAt Problem 1 Answer