Example #1
0
    void move()
    {
        CharacterController controller = GetComponent <CharacterController>();

        roleSprite = GetComponent <tk2dSprite>();

        // Player move
        if (controller.isGrounded)
        {
            //Debug.Log(Input.GetAxis("Horizontal"));
            moveDirection  = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
            moveDirection  = transform.TransformDirection(moveDirection);
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }

        if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.H))
        {
            if (!anim.IsPlaying("leftwalk"))
            {
                anim.Play("leftwalk");
            }
        }
        else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.H))
        {
            anim.Play("leftstop");
        }
        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.L))
        {
            if (!anim.IsPlaying("rightwalk"))
            {
                anim.Play("rightwalk");
            }
        }
        else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.L))
        {
            anim.Play("rightstop");
        }

        moveDirection.y -= gravity * Time.deltaTime;
        // Move the controller
        controller.Move(moveDirection * Time.deltaTime);
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.A))
        {
            // Only play the clip if it is not already playing.
            // Calling play will restart the clip if it is already playing.
            if (!anim.IsPlaying("hit"))
            {
                anim.Play("hit");

                // The delegate is used here to return to the previously
                // playing clip after the "hit" animation is done playing.
                anim.animationCompleteDelegate = HitCompleteDelegate;
            }
        }

        if (Input.GetKey(KeyCode.D))
        {
            if (!anim.IsPlaying("walk"))
            {
                // Walk is a looping animation
                // A looping animation never completes...
                anim.Play("walk");

                // We dont have any reason for detecting when it completes
                anim.animationCompleteDelegate = null;
                walking = true;
            }
        }

        if (Input.GetKey(KeyCode.W))
        {
            if (!anim.IsPlaying("idle"))
            {
                anim.Play("idle");
                anim.animationCompleteDelegate = null;
                walking = false;
            }
        }
    }
Example #3
0
    void walk()
    {
        sprite.transform.localScale = new Vector3(-attackDirection.x - attackDirection.y, 1, 1);

        var movement    = walkDirection * speed * Time.deltaTime;
        var newPosition = transform.position + new Vector3(movement.x, 0, movement.y);

        if (transform.position == newPosition)
        {
            sprite.Stop();
        }
        else if (!sprite.IsPlaying(sprite.CurrentClip))
        {
            sprite.Play();
        }

        if (canWalk(newPosition))
        {
            transform.position = newPosition;
        }
    }