# 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 :

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);
``````

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);
``````

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: