Simple Rotation in 2D SHMUP

Hi. I’m trying to get a simple player move/rotation executed when the player moves left or right. I have a fixed perspective top down shooter, similar in style (not awesomeness) to Air Attack HD. This video does a good job of displaying what I’m looking for. When the player moves left or right, his ship also rotates on the XY? Axis. When the player stops moving left or right, the ship’s rotation snaps back to the center. I have figured out how to perform the initial move/rotation, but I can’t figure out how to make the player ship snap back after the left right move keys are no longer being pressed. I appreciate any help you all have, as i’m still fairly new to Unityscript.

What I believe you need to do here is store your previous position in your script, then compare the previous to current to determine if you are moving. You use the difference between the prev and the current to set the aircraft’s rotation. kind of like

rot transform = currentTransform - prevTransform
prevTransform = currentTransform.

Offhand I cant write the code but I saw your tweet and hopefully this gets you on the right track.

even easier. your angle of z rotation relies on the difference between the current position and the target position (or specifically the InverseTransformPoint().x of the target position) Then do some simple math and you get the Z rotation. Since were at it, here is some example code:

``````var hSpeed=10.0;

var plane : Plane=Plane(Vector3.up, transform.position);
var ray : Ray = camera.ScreenPointToRay (Input.mousePosition);
var dist=0.0;
plane.Raycase(ray, dist);
var targetPosition=ray.origin + ray.direction * dist;
transform.localEulerAngles.z=transform.InverseTransformPoint(targetPosition).x / 10.0;
transform.position=Vector3.Lerp(transform.position, targetPosition, hSpeed * Time.deltaTime);
``````

Thanks for the responses guys!

Here’s what I have for Code

``````var  playerhorizontal  = (playerSpeed * Input.GetAxis ("Horizontal") )* Time.deltaTime;
horizontalmove = new Vector3(0,0,1);
transform.Translate(horizontalmove * playerhorizontal);
transform.Rotate(playerhorizontal, 0, 0);
``````

The above handles the player’s movement. The transform rotate uses the same variable player horizontal to determine the angle.

I then have a call to the PlayerRotate function from update:

``````function PlayerRotate()
{
if (transform.localEulerAngles.x > 10)
{
WaitForSeconds (1);
transform.Rotate(-1,0,0);
}

}
``````

The logic here I need something to check to see if the player has rotated to a certain angle, and then puts the player back at his original rotation. This doesn’t work. What am I missing?

I got it! Thanks again for your help! I actually ended up taking this from the UnityScript Manual.

``````var smooth = 2.0;
var tiltAngle = -30.0;
``````

Set the angle of tilt. And then attach that to a GetAxis controller instruction with a variable.

``````function Update () {

var tiltAroundX= Input.GetAxis("Horizontal") * tiltAngle;
var tiltAroundZ = Input.GetAxis("Vertical") * tiltAngle;
var target = Quaternion.Euler (tiltAroundX, 180, tiltAroundZ);

// Dampen towards the target rotation
transform.rotation = Quaternion.Slerp(transform.rotation, target,
Time.deltaTime * smooth);};
``````