# Space shooter enemy movement (tilt when turning)

Hi!

I have enemies that move towards the player using LookRotation and I have been trying to get the enemies tilt when they are turning (turning is done on Y axis). I’m currently not using any force (physics) to move the enemies because I need them to maintain static speed even when turning and with using force I think I can’t achieve that, although it could be more realistic to use force since each ship has thrusters.

Anyways I have been banging my head to the wall with this and I was hoping that someone could give me some pointers how to do this.

I have seen some examples where player controlled ship is tilted when moving left or right using velocity but my enemies are AI controlled.

Here’s how the enemies move towards the player:

``````transform.position += transform.forward * movementSpeed * Time.deltaTime;
``````

And here’s one example on how I’ve tried to get them tilting while turning using some deltaY (this is done in FixedUpdate):

I think that I would need to include the tilt rotation to the rotation that I get from LookRotation.

``````        float deltaY = Mathf.DeltaAngle(transform.rotation.y, previousY);

// Look at the player
Quaternion lookRot = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(targetPosition - transform.position), rotationSpeed * Time.deltaTime);

float rotationChange = 10; // just to see if there's any rotation happening

if (deltaY < 0)
{
rotationChange *= -1;
}
else
{
rotationChange = Mathf.Abs(rotationChange);
}

Quaternion tiltRot = Quaternion.AngleAxis(deltaY * rotationChange, Vector3.forward);

transform.rotation = lookRot * tiltRot;

previousY = transform.rotation.y;
``````

I appreciate any help that you can provide

I would like to be able to control what is the maximum tilt that a ship can do while turning (something like 90 degrees) and the amount of tilt should be relative to how tight the turning is if you know what I mean.

If you think that this is easier to do with using physics I’m open to any suggestions.

Br,

Kalle

Quaternions are confusing, and I would use Euler angles.
For example

``````float lookRot = Mathf.LerpAngle(a1, a2, t);
float bank = deltaY * rotationChange;
transform.localEulerAngles = new Vector3(bank, lookRot, 0f);
``````

Hey!

Could you please explain in a bit more detail which angles to use with LerpAngle a1, a2 and how to get them?

Also how would you calculate this deltaY?

I need to have rotation to player in Y-axis and in addition change the local rotation in Z-axis (with max 90 degrees) for the tilt I guess.

Br,

Kalle

a1 is your original angle, and a2 is your final angle. It’s working like Quaternion.Slerp, except that you are only calculating one angle, and use it to rotate around the y-axis. You could use

``````float a1 = transform.localEulerAngles.y;
a2 = Mathf.Rad2Deg * Mathf.Atan2(targetPosition.x - transform.position.x, targetPosition.z - transform.position.z);
``````

There might be another way to calculate a2 with lookAt, but it’s possible to calculate the angle on the y axis with trig.

What you have for deltaY looks ok or you could use: `float deltaY = Mathf.DeltaAngle(a1, lookRot);`