Smooth IK look at object issue.

I’m attempting to have my characters weapon look at a point in space smoothly. When any axis changes drastically the smooth movement is lost and a snapping occurs.

I’m using Vector3.MoveTowards() to go from current look at position, to goal. I believe the issue is being presented because MoveTowards is probably trying to make all axis reach target position at the same time. So if X is 0 and goal is 10, while Y is 0 and goal is 50, X will reach 10 the same frame Y reaches 50 even though the distances are much further apart.

I’ve been unable to figure a solution out and researching this hasn’t brought me very far.

Here’s a sample video:

You’ll notice the movement is fluid when I move the crosshairs along the wall. But when I go from the wall to a closer object the movement drags then snaps. The select object you see in the scene is the goal look at position.

Looks like you’re lerping between vectors in world space and then just setting the rotation to look at the interpolated vector. That means it has to travel in the z as well as x and y, which isn’t what you want (we don’t take longer to look at things that are far away, it’s the ‘angular distance’ that matters).

Try normalising the vectors beforehand, or just do the lerp with the rotation instead by using Quaternion.Lerp.

I’m not sure a quaternion lerp would work as I’m basically patching up an asset to support smooth follow rather than snapping. An asset I barely understand to be honest, otherwise I’d done the work myself. In the code it updates the look at position by using the position of a transform(target). I was hoping ‘look at position = MoveTowards target’ would work but as you can see it provides unreliable results.

Could you provide me with an example how normalizing would be used in this scenario? I’m familiar with what normalizing is, just not sure how it could work here. Thank you.

I resolved this by intersecting and overshooting the aim at position, causing depth to always be the same and in result MoveTowards only having to move on the X and Y axis.

(image is approximate)

Example:
float castDistance = 100f;
RaycastHit hit;
Vector3 camHit = Raycast(ray, out hit, castDistance);

//weaponStart is approximate to where weapon would rest against a characters shoulder.
Vector3 direction = (hit.point - weaponStart.position).normalized;
Vector3 aimAt = weaponStart + (direction * castDistance);

Then you execute your IK to aim towards aimAt.