# Question Need help with Quaternions, please - Make a model spin while looking to a direction.

Discussion in 'Getting Started' started by rafael_lima83, Oct 29, 2023.

1. ### rafael_lima83

Joined:
Sep 6, 2023
Posts:
9
Ok, the documentation tells me I should try to not understand Quaternions and just use the methods Unity give me.

I tried it, I am still really very lost.

I'm trying to make an enemy look at the player and while, it keeps "looking" at the direction of the player, tts whole body is spinning on his axis

I can make a model keep "looking" at the other. I did it with Quaternion.Euler, with Quaternion.FromToRotation and with Quaternion.LookRotation.

But make it look at the player *and* keeping the body spinning like that.... I just can't. Whatever I try the rotation just goes crazy to different directions...

Anyone has any idea of the math behind that? Thank you a lot in advance.

Last edited: Oct 29, 2023
2. ### rafael_lima83

Joined:
Sep 6, 2023
Posts:
9
Ok, again, found the solution myself using the LookAt method and *then* applying the rotation I want, instead of trying to do both things using Quaternion methods.

It looks something like this:

Code (CSharp):
1.   public Transform target2;
2.   public float rotationSpeed = 45f;
3.
4. void SpinBody()
5. {
6.      transform.LookAt(player.transform.position,Vector3.forward);
7.      Quaternion currentRotation = transform.rotation;
8.      rotationAmount += rotationSpeed * Time.deltaTime;
9.      Quaternion spinRotation = Quaternion.Euler(0f, 0f,  rotationAmount);
10.      Quaternion newRotation = currentRotation * spinRotation;
11.      transform.rotation = newRotation;
12. }
Of course you need to adapt to your solution if you trying to copy this code, and "player" is a Serialized GameObject field that points to the player.

3. ### spiney199

Joined:
Feb 11, 2021
Posts:
7,360
Definitely kudos for working out quaternion rotations and combining them via multiplication. It's something a lot of new users struggle with.

One suggestion is to use Quaternion.AngleAxis as opposed to a euler rotation around a specific axis, which produces a rotation upon the axis of the direction supplied. So should it need to look at any direction other than
``Vector3.forward``
, it can still rotate around the direction it points at.

rafael_lima83 likes this.