- Home /
How do I translate my ship left and right and have it rotate at the same time?
SO essentially Im working on a top down 3d shooter, and I have a ship that I want to move side to side and up and down, I HAVE THIS WORKING, but there is a new feature that I would like to add, I would like the ship to rotate towards the direction that it is turning and then when the user is done turning it goes back to its original rotation orientation.
Here is my code so far
using UnityEngine; using System.Collections;
public class PlayerScript: MonoBehaviour { // players lives private int playerLife = 5; private float speed = 8.0f; private int missleCount = 3; public float rotateSpeed;
public static int playerScore; //public static int missles;
public Rigidbody projectileBullet; public Rigidbody projectileMissle;
void OnGUI()//Works with the GUI interface { //Score that shows on screen GUI.Label(new Rect(10,10,200,50), "Score: " + playerScore);
//Players Life
GUI.Label(new Rect(10,30,200,50), "Lives: " + playerLife);
GUI.Label(new Rect(10, 50, 200, 50), "Missles: " + missleCount);
}
public int getPlayerLife { get{ return playerLife;} }
public float getSpeed { get{ return speed;} }
// Update is called once per frame void Update () {
//amount to movement
float movementHorizontal = (speed * Input.GetAxis("Horizontal")) * Time.deltaTime;
float movementVertical = (speed * Input.GetAxis("Vertical")) * Time.deltaTime;
transform.Translate(Vector3.right * movementHorizontal);
//translate the player Horizontaly
transform.Translate(Vector3.right * movementHorizontal);
//translate vertically
transform.Translate(Vector3.up * movementVertical);
if (transform.position.x <= -7.5)
{
transform.position = new Vector3(7.4f, transform.position.y, transform.position.z) ;
}
else if (transform.position.x >= 7.5)
{
transform.position = new Vector3(-7.4f, transform.position.y, transform.position.z);
}
if (Input.GetKeyDown(KeyCode.Space))
{
Rigidbody cloneBullet;
cloneBullet = Instantiate(projectileBullet, transform.position, transform.rotation) as Rigidbody;
}
if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
{
Debug.Log(missleCount);
if (missleCount >= 0 && missleCount != 0)
{
--missleCount;
Rigidbody cloneMissle;
cloneMissle = Instantiate(projectileMissle, transform.position, transform.rotation) as Rigidbody;
}
}
}
}
Please consider giving your question a title that indicates what the problem is, rather than YELLING AT US ABOUT YOUR BIG PROBLE$$anonymous$$. Cheers.
Answer by Loius · Oct 08, 2010 at 02:27 AM
Rotation is accomplished through transform.rotation. You can find all you need about rotation in the Script Reference; just search for "rotation".
While your player is moving right, rotate towards +X* on the ship's up axis; while moving left, go towards -X*.
I have another question? Sorry T.T How do you reset the ships rotation position after it has translated and rotated towards the direction?
You probably don't need to "reset" your rotation as much as you need to just move the ship forwards or up along the current rotation.
So by default, if your ship is facing up, then any rotations you apply you can Translate the ship up as shown in the example on the following page, http://unity3d.com/support/documentation/ScriptReference/Transform.Translate.html