Sunday, March 9, 2014

Player Script

//Player script

//Inspector variables
var lives                     : int = 3;                       // Player life
var playerSpeedHorizontal     : float = 10.0;             // Player speed on x axis
var playerSpeedVertical       : float = 10.0;             // Player speed on y axis
var horizontalMin             : float = -10;              // limit left
var horizontalMax             : float = 10;               // limit right
var VerticalMin               : float = -10;              // limit down
var VerticalMax               : float = 10;               // limit up
var bullet                    : Transform;
var socket                    : Transform;
var numberofShields  :int = 4;                   // number of shield
var shieldMesh                : Transform;                    
var shieldKeyInput            :KeyCode;                  

//Private variables
private var shieldOn  :boolean = false;

// loop / repeat
function Update ()
{
 //Input Variables
 var transV : float = Input.GetAxis("Vertical")*playerSpeedVertical*Time.deltaTime;        //store varible for verticle movement
 var transH : float = Input.GetAxis("Horizontal") *playerSpeedHorizontal*Time.deltaTime;   // store variable for horizontal movement

//move player base on input
transform.Translate(transH,transV,0);   // x = left and right & y = up and down

//player position limit

transform.position.x=Mathf.Clamp(transform.position.x,horizontalMin,horizontalMax);       // limit to the left and right

transform.position.y=Mathf.Clamp(transform.position.y,VerticalMin,VerticalMax);           // limit for up and down

//Bullet
if(Input.GetKeyDown("space"))
 {
Instantiate(bullet,socket.position,socket.rotation);
 }

  // Create shield
 if(Input.GetKeyDown(shieldKeyInput))
 {
  if (!shieldOn) // no more shield can be created if the current one still exist
  {
var clone = Instantiate(shieldMesh,transform.position,transform.rotation);
clone.transform.parent = gameObject.transform;    // parent the object to selected object
shieldOn = true;
}
 }
}

No comments:

Post a Comment