Z axis tilt while following player

Hey guys,

I’m working on a 2d overhead space shooter, and I’ve got all my tilting looking good on the player ship (I know, I know, real physics in space wouldn’t tilt). The problem I’m having is the enemy ships. I have them chase the player using the default SmoothLookAt script and constantforce within a certain radius.

How can I convert the SmoothLookAt angle to a z-axis rotation that rights itself as the angle decreases between the enemy ship and the player? I’ve tried a heap of potential solutions, but can’t seem to get it right.

Right now I’m trying to do it this way (with the parent GO using the SmoothLookAt script):

function FixedUpdate() { 
	
	var rotation = Quaternion.LookRotation(target.position - shellObject.position);
	
	print (rotation);
	
	 if (rotation.y < 40) {	
	
		transform.localRotation.z += rotation.x - rotation.z;
		
	 } else {
	 	this.transform.localRotation.z = Mathf.Lerp(this.transform.localRotation.z, 0.0, Time.deltaTime * 3);
	 }
	
}

Any help is appreciated :slight_smile:

Seriously? No one? I thought this would be an easy one :slight_smile:

Here’s how i got it basically working (though I’m still trying to lerp it into the tilt first):

var rotation : Quaternion = Quaternion.LookRotation(Vector3(target.position.x, 1.25, target.position.z) - shellObject.position);
transform.localRotation.eulerAngles.z = Mathf.Clamp(shellObject.rotation.eulerAngles.y - rotation.eulerAngles.y, -45, 45);

Any suggestions?

Well, I (personally) didn’t answer initially because I wasn’t really clear on what type of behavior you’re trying to implement (and I’m still not quite clear on that).

Oh, thanks Jesse :slight_smile:

Basically we’re looking down on our ships, and they rotate to face their target. I want them to tilt on the z-axis as if they are banking toward their target, and right themselves as the angle decreases. I’ve got it working that way at the moment, but with this method it just snaps to the deepest bank possible (45 degrees), then recovers. What I’d like is for it to ease in, then ease back.

Use eular rotation instead?

I am, it’s just the initial dip that’s weird. Imagine the timeline of what I want like this:

start tilting-----------full tilt------------ease off tilt

But what’s happening is this:

full tilt ----------- ease off tilt

It seems like I should be able to do it with Lerping its current value toward the tilt value, but that just messes everything up.