Create a new folder in assets and name it Scripts, Then click on the player in the hierachy and add component scripts, new script. and name it PlayerController, then drag and drop the script into the Scripts folder and open it up in monoDevelop. Remove the sample code.
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<rigidbody>().velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax),
);
GetComponent<rigidbody>.rotation = quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
This code allows for changing the speed that the player ship will move but also sets a boundary so that the player cant go off the edge of the screen as well as making it changeable within unity by Serializing the code. The boundary can now be altered in the inspector in unity, set the Xmin to -6 and Xmax to 6 with the zMin at -4 and the zMax at 8. Next set the tilt to 5 this gives nice banking effect when moving from side to side.
Comments
Post a Comment