Beispiel #1
0
    private void Move()
    {
        isGrounded = Physics2D.BoxCast(
            transform.position,
            new Vector2(2.0f, 1.0f), 0.0f,
            Vector2.down, 1.0f,
            movementLayerMask);

        // Idle State
        if (Input.GetAxis("Horizontal") == 0)
        {
            heroAnimState = HeroAnimState.IDLE;
            heroAnimator.SetInteger("AnimState", (int)HeroAnimState.IDLE);
        }


        // Move Right
        if (Input.GetAxis("Horizontal") > 0)
        {
            //heroSpriteRenderer.flipX = false;
            transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);

            if (isGrounded)
            {
                heroAnimState = HeroAnimState.WALK;
                heroAnimator.SetInteger("AnimState", (int)HeroAnimState.WALK);
                //heroRigidBody.AddForce(Vector2.right * moveForce);
                heroRigidBody.AddForce(new Vector2(1, 1) * moveForce);
            }
        }

        // Move Left
        if (Input.GetAxis("Horizontal") < 0)
        {
            //heroSpriteRenderer.flipX = true;
            transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
            if (isGrounded)
            {
                heroAnimState = HeroAnimState.WALK;
                heroAnimator.SetInteger("AnimState", (int)HeroAnimState.WALK);
                //heroRigidBody.AddForce(Vector2.left * moveForce);
                heroRigidBody.AddForce(new Vector2(-1, 1) * moveForce);
            }
        }

        // Jump
        if ((Input.GetAxis("Jump") > 0) && (isGrounded))
        {
            heroAnimState = HeroAnimState.JUMP;
            heroAnimator.SetInteger("AnimState", (int)HeroAnimState.JUMP);
            heroRigidBody.AddForce(Vector2.up * jumpForce);
            isGrounded = false;
            jumpSound.Play();
        }

        heroRigidBody.velocity = new Vector2(
            Mathf.Clamp(heroRigidBody.velocity.x, -maximumVelocity.x, maximumVelocity.x),
            Mathf.Clamp(heroRigidBody.velocity.y, -maximumVelocity.y, maximumVelocity.y)
            );
    }
Beispiel #2
0
    /// <summary>
    /// This method is responsible for moving character and checikng if it is on the ground
    /// </summary>
    public void Move()
    {
        grounded = Physics2D.BoxCast(transform.position, new Vector2(0.2f, 0.1f), 0.0f, Vector2.down, 0.3f, 1 << LayerMask.NameToLayer("Ground"));

        if ((Input.GetAxis("Horizontal") > 0) && (grounded))
        {
            spriteRenderer.flipX = false;
            heroAnimState        = HeroAnimState.WALK;
            anim.SetInteger("AnimState", (int)HeroAnimState.WALK);
            rgb.AddForce(Vector2.right * speed);
        }
        if ((Input.GetAxis("Horizontal") < 0) && (grounded))
        {
            spriteRenderer.flipX = true;
            heroAnimState        = HeroAnimState.WALK;
            anim.SetInteger("AnimState", (int)HeroAnimState.WALK);
            rgb.AddForce(Vector2.left * speed);
        }

        if (Input.GetAxis("Horizontal") == 0 && (grounded))
        {
            heroAnimState = HeroAnimState.IDLE;
            anim.SetInteger("AnimState", (int)HeroAnimState.IDLE);
        }
        if ((Input.GetAxis("Jump") > 0) && (grounded))
        {
            anim.SetInteger("AnimState", (int)HeroAnimState.JUMP);
            heroAnimState = HeroAnimState.JUMP;
            rgb.AddForce(Vector2.up * jumpForce);
            grounded = false;
        }
        rgb.velocity = new Vector2(Mathf.Clamp(rgb.velocity.x, -maxVel.x, maxVel.x), Mathf.Clamp(rgb.velocity.y, -maxVel.y, maxVel.y));
    }
    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics2D.Linecast(
            transform.position, groundTarget.position,
            1 << LayerMask.NameToLayer("Ground"));

        isGrounded = Physics2D.BoxCast(
            transform.position, new Vector2(2.0f, 1.0f), 0.0f, Vector2.down, 1.0f,
            1 << LayerMask.NameToLayer("Ground"));

        //Idle
        if (Input.GetAxis("Horizontal") == 0)
        {
            heroAnimState = HeroAnimState.IDLE;
            heroAnimator.SetInteger("AnimState", (int)HeroAnimState.IDLE);
        }
        //Move Right
        if (Input.GetAxis("Horizontal") > 0)
        {
            heroSpriteRenderer.flipX = false;
            if (isGrounded)
            {
                heroAnimState = HeroAnimState.WALK;
                heroAnimator.SetInteger("AnimState", (int)HeroAnimState.WALK);
                heroRigidBody.AddForce(Vector2.right * moveForce);
            }
        }

        //Move Left
        if (Input.GetAxis("Horizontal") < 0)
        {
            heroSpriteRenderer.flipX = true;
            if (isGrounded)
            {
                heroAnimState = HeroAnimState.WALK;
                heroAnimator.SetInteger("AnimState", (int)HeroAnimState.WALK);
                heroRigidBody.AddForce(Vector2.left * moveForce);
            }
        }

        //Jump
        if ((Input.GetAxis("Jump") > 0) && (isGrounded))
        {
            heroAnimState = HeroAnimState.JUMP;
            heroAnimator.SetInteger("AnimState", (int)HeroAnimState.JUMP);
            heroRigidBody.AddForce(Vector2.up * jumpForce);
            isGrounded = false;
            jumpSound.Play();
        }
    }
Beispiel #4
0
    public void ShotAnimation(HeroAnimState anim_state)
    {
        switch (anim_state)
        {
        case HeroAnimState.walking:
        {
            animator.Play(Animator.StringToHash("Hero_Walking"), 0);
            break;
        }

        case HeroAnimState.damage:
        {
            animator.Play(Animator.StringToHash("Hero_Damage"), 0);
            break;
        }

        case HeroAnimState.freeze:
        {
            animator.Play(Animator.StringToHash("Hero_Freeze"), 0);
            break;
        }
        }
    }
Beispiel #5
0
 // Start is called before the first frame update
 void Start()
 {
     heroAnimState = HeroAnimState.IDLE;
     isGrounded    = false;
 }
        // Update is called once per frame
        void Update()
        {
            Debug.DrawRay(transform.position, Vector2.down * 2, Color.yellow);

            //RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down * 2);

            //Debug.Log(hit.collider.gameObject.name);

            bool hit = Physics2D.Linecast(
                transform.position,
                groundTarget.position,
                1 << LayerMask.NameToLayer("ground"));

            bool die = Physics2D.Linecast(
                transform.position,
                groundTarget.position,
                1 << LayerMask.NameToLayer("water"));

            if (die)
            {
                dead = true;
            }

            if (hit)
            {
                grounded = true;
            }
            else
            {
                grounded = false;
            }
            // idle

            if (Input.GetAxis("Horizontal") == 0)
            {
                animator.SetInteger("AnimState", (int)HeroAnimState.IDLE);
                heroAnimState = HeroAnimState.IDLE;
            }

            // moving to the right
            if ((Input.GetAxis("Horizontal") > 0) && (grounded))
            {
                animator.SetInteger("AnimState", (int)HeroAnimState.WALK);
                heroAnimState        = HeroAnimState.WALK;
                spriteRenderer.flipX = false;
                playerrb2d.AddForce(Vector2.right * 35.0f);
            }

            // moving to the left
            if ((Input.GetAxis("Horizontal") < 0) && (grounded))
            {
                animator.SetInteger("AnimState", (int)HeroAnimState.WALK);
                heroAnimState        = HeroAnimState.WALK;
                spriteRenderer.flipX = true;
                playerrb2d.AddForce(Vector2.left * 35.0f);
            }

            // jumping
            if ((Input.GetAxis("Jump") > 0) && (grounded))
            {
                animator.SetInteger("AnimState", (int)HeroAnimState.JUMP);
                heroAnimState = HeroAnimState.JUMP;
                playerrb2d.AddForce(Vector2.up * 200.0f);
            }

            // not jumping
            if (Input.GetKeyUp(KeyCode.Space))
            {
                animator.SetInteger("AnimState", (int)HeroAnimState.IDLE);
                heroAnimState = HeroAnimState.IDLE;
            }

            if ((transform.position.y < bounds.TopBounds) || (dead))
            {
                Reset();
                controller.Lives -= 1;
                dead              = false;
            }
        }
 // Start is called before the first frame update
 void Start()
 {
     Reset();
     heroAnimState        = HeroAnimState.IDLE;
     spriteRenderer.flipX = true;
 }
Beispiel #8
0
    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector3(
            transform.position.x,
            transform.position.y,
            0.0f);

        //bool hit = Physics2D.Linecast(
        //        transform.position,
        //        groundTarget.position,
        //        1 << LayerMask.NameToLayer("Ground"));


        isGrounded = Physics2D.BoxCast(
            transform.position,
            new Vector2(2.0f, 1.0f), 0.0f,
            Vector2.down, 1.0f,
            1 << LayerMask.NameToLayer("Ground"));


        // Idle
        if (Input.GetAxis("Horizontal") == 0)
        {
            animator.SetInteger("AnimState", (int)HeroAnimState.IDLE);
            heroAnimState = HeroAnimState.IDLE;
        }


        // moving to the right
        if ((Input.GetAxis("Horizontal") > 0) && (isGrounded))
        {
            animator.SetInteger("AnimState", (int)HeroAnimState.WALK);
            heroAnimState        = HeroAnimState.WALK;
            spriteRenderer.flipX = false;
            playerRigidBody.AddForce(Vector2.right * 30.0f);
        }

        // moving to the left
        if ((Input.GetAxis("Horizontal") < 0) && (isGrounded))
        {
            animator.SetInteger("AnimState", (int)HeroAnimState.WALK);
            heroAnimState        = HeroAnimState.WALK;
            spriteRenderer.flipX = true;
            playerRigidBody.AddForce(Vector2.left * 30.0f);
        }

        // jumping
        if ((Input.GetAxis("Jump") > 0) && (isGrounded))
        {
            animator.SetInteger("AnimState", (int)HeroAnimState.JUMP);
            heroAnimState = HeroAnimState.JUMP;
            playerRigidBody.AddForce(Vector2.up * jumpForce);
            jumpSound.Play();
            isGrounded = false;
        }

        // not jumping
        if (Input.GetKeyUp(KeyCode.Space))
        {
            animator.SetInteger("AnimState", (int)HeroAnimState.IDLE);
            heroAnimState = HeroAnimState.IDLE;
        }

        playerRigidBody.velocity =
            new Vector2(
                Mathf.Clamp(playerRigidBody.velocity.x,
                            -maximumVelocity.x, maximumVelocity.x),
                Mathf.Clamp(playerRigidBody.velocity.y,
                            -maximumVelocity.y, maximumVelocity.y));
    }
Beispiel #9
0
 public bool grounded;                  // Is used to represent boolean(true or false) value to check if the hero is on the ground
 // Start is called before the first frame update
 void Start()
 {
     rgb           = GetComponent <Rigidbody2D>(); // Is used to get Rigidbody2D component
     heroAnimState = HeroAnimState.IDLE;           // Sets animation of the hero to Idle
 }