Hello,
I'm trying to create a top down 3d Shooter. (Air attack HD) I'm trying to figure out how to get the player's ship to "roll" left or right when the player uses the horizontal keys. So far, I've figured this out:
1) Check to see if the player has pressed the "D" Horizontal Move key 2) Move the player's ship (Done in another part of the code) 3) Rotate the player's ship to -35 degrees 4) Wait some time 5) Move the player's ship back to original rotation
The problem is, the ship object never rotates back to it's normal rotation/position. How do I fix this. Here's the code that I have so far.
function PlayerShipRoll ()
{
//////Fix this!
// value = Input.GetKey ("W");
if (Input.GetKey (KeyCode.A))
{
transform.Rotate(0, 0,0);
yield ;
transform.Rotate(0, 0,0);
//transform.rotation.x = Mathf.Clamp(transform.rotation.x, 25, 0);
print ("Horizontal key was pressed");
}
if (Input.GetKeyDown (KeyCode.D))
{
transform.Rotate(-35,0, 0);
transform.rotation.x = Mathf.Clamp(transform.rotation.x, 0 ,0 );
yield WaitForSeconds(2) ;
transform.Rotate (180,180,0);
}
}
For a start, transform.rotation is a quaternion, and rotation.x isn't what you're thinking. Use eulerAngles, or Quaternion.Euler.
– Eric5h5There are a number of answered Q's for pretty much the same thing on here. If that worked, it would "snap" tilted, then back. Most are using Lerp to get a gradual move. Search around for that stuff.
– Owen-ReynoldsThanks for the Response Guys. I actually came up with the below. This works, but I'm having trouble getting it to "snap" in place. The player's ship rotates, and goes back to it's original rotation. The lerp idea sounds cool, but Quaternion doesn't seem to work with a transform. Any thoughts? if(Input.GetKeyDown (KeyCode.D)) { transform.Rotate(-45,0,0); transform.rotation.x = Mathf.Clamp(-50, 0 ,0 ); yield WaitForSeconds(2) ; transform.Rotate (0,0,0); }
– anon40666834Look up clamp. It's just a shortcut for saying "can't be less than X or more than Y." Rotate(0,0,0) is pretty much the same thing as adding 0. Look at Euler angles to say "this is my new rotation."
– Owen-ReynoldsThanks Owen makes sense but I want the object to go back to it's original position which would be 0 0 0 or close to it
– anon40666834