Search Unity

Scripting 2D melee script

Discussion in 'Scripting' started by avtar109, Mar 14, 2014.

  1. avtar109

    avtar109

    Joined:
    Mar 14, 2014
    Posts:
    4
    #pragma strict
    var Anim : Animator;
    var MaxSpeed : float = 5.0f;
    var MoveForce : float = 356.0f;
    var Direction : boolean = false;
    private var Health : int = 10;
    var Shoot : boolean = false;
    var Grounded : boolean;
    var GroundChecker : Transform;
    var Jumpforce : int = 1000;
    var Attack : boolean = false;
    var Jump : boolean = false;
    var MoveMulti : int = 0;

    function Update () {
    Grounded = Physics2D.Linecast(transform.position , GroundChecker.position, 1<<LayerMask.NameToLayer("Ground")); //Takes start vector 3 position, end vetor 3 position, layer number, layer mask name returns true if hit something

    if (Input.GetButtonDown("Jump") Grounded) //if groudned and space pressed do code
    {
    Jump = true;
    }
    }

    function FixedUpdate (){

    var H : float = Input.GetAxis ("Horizontal"); //H = horizontal key input 0 - 1

    if (H > 0.1 Input.GetKey(KeyCode.LeftShift))
    {
    MaxSpeed = 10;
    }
    else if (H < 0.1 Input.GetKey(KeyCode.LeftShift))
    {
    MaxSpeed = 10;
    }
    else if(!Input.GetKey(KeyCode.LeftShift))
    {
    MaxSpeed = 5;
    }



    if(Input.GetKey(KeyCode.LeftShift))
    {

    Anim.SetBool ("Run",true);
    }
    else
    {
    Anim.SetBool ("Run",false);
    }

    Anim.SetFloat ("Walk",Mathf.Abs (H)); //Sets Run parameter to absolute value of H horizontal 0-1

    if(H*rigidbody2D.velocity.x <MaxSpeed){ // if H is 0 then times = 0 so doesnt run if has value it rusn = checks if key down but with math smart ay
    rigidbody2D.AddForce(Vector2.right*H*MoveForce); // same as above but actualy adds velocity if not H 0
    }

    if(Mathf.Abs(rigidbody2D.velocity.x)>MaxSpeed)
    {
    rigidbody2D.velocity = new Vector2 (Mathf.Sign(rigidbody2D.velocity.x)*MaxSpeed, rigidbody2D.velocity.y);
    }

    //tests flipping using 0-1 and 0- -1 and boolean Direction to see if facing right or left
    if(H < 0 !Direction)
    {
    Flip();
    }
    else if (H > 0 Direction)
    {
    Flip();
    }

    if(Jump)
    {
    rigidbody2D.AddForce (new Vector2(0f, Jumpforce)); // adds jumpforce to y x is 0
    Anim.SetBool("Jump", true);
    Jump = false;
    StopJump();
    }
    }

    function Flip()
    {
    Direction = !Direction;
    var TheScale : Vector3 = transform.localScale; // Takes local objects transform as new vector3

    TheScale.x *=-1;
    transform.localScale = TheScale;
    }

    function StopJump()
    {
    yield WaitForSeconds (0.8);
    Anim.SetBool("Jump", false);

    }