- Home /
How to create a player movement with Rotation?
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.
There are a number of answered Q's for pretty much the same thing on here. If that worked, it would "snap" tilted, then back. $$anonymous$$ost are using Lerp to get a gradual move. Search around for that stuff.
Thanks 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.Get$$anonymous$$eyDown ($$anonymous$$eyCode.D))
{
transform.Rotate(-45,0,0);
transform.rotation.x = $$anonymous$$athf.Clamp(-50, 0 ,0 );
yield WaitForSeconds(2) ;
transform.Rotate (0,0,0);
}
Look 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."
Thanks 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
Answer by Marvin Hawkins · May 03, 2011 at 11:46 AM
This is not an actual answer, but I haven't figured this out, and can still use some help.
var playerhorizontal = (playerSpeed * Input.GetAxis ("Horizontal") )* Time.deltaTime;
horizontalmove = new Vector3(0,0,1);
transform.Translate(horizontalmove * playerhorizontal); transform.Rotate(playerhorizontal, 0, 0); enter code here
The transform.Rotate(playerhorizontal,0,0); line does the trick, but I still can't get the ship to snap back into place. Any thoughts?
This video does the best job of showing the type of movement that I'm looking for. It shows the ship movement from side to side with a slight "banking" rotation. Any thoughts?