# How to use Quaternion.Slerp with transform.LookAt?

I’m creating a game where you need to “lock on” to your target in order to properly aim your attacks. I’ve already made working code where you can move your player around, make him jump, and make him lock on to the enemy.

The problem is, whenever I lock onto the target, the player object rotates immediately. It finishes the rotation in a single frame. In a slightly more realistic setting, you would look at the target by turning around over the span of half a second or so. This is what I want to achieve with the player object.

From what I understand, Quaternion.Slerp is a good tool for this kind of thing. I already use it in my player movement:

``````float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");

if (moveHorizontal > 0 || moveHorizontal < 0 || moveVertical > 0 || moveVertical < 0)
{
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);

transform.Translate (movement * moveSpeed * Time.deltaTime, Space.World);
}
``````

But I can’t seem to make it work with my lock on code.

Here’s my current lock on code, working version without all the Slerp experiments:

``````if (lockOnToggle == "yes")
{
Vector3 lockOnLook = new Vector3 (enemy.transform.position.x, transform.position.y, enemy.transform.position.z);
transform.LookAt(lockOnLook);
}
``````

Both of these blocks of code are in void Update().

I’ve tried several methods by googling, such as converting Vector3 to Quaternion and other crazy code stuff, but all the results that came up were confusing and I couldn’t get them to work.

How would I go about making this work?

Try:

``````  if (lockOnToggle == "yes")
{
Quaternion lookOnLook =
Quaternion.LookRotation(enemy.transform.position - transform.position);

transform.rotation =
Quaternion.Slerp(transform.rotation, lookOnLook, Time.deltaTime);
}
``````

thanks charmingly working

This worked for me as well, thank you!