Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Polygon Collider 2D bugs my jump aimation.

Discussion in '2D' started by DutchGameNerd, Aug 29, 2019.

  1. DutchGameNerd

    DutchGameNerd

    Joined:
    Aug 24, 2019
    Posts:
    3
    Hello, I have a problem with the Polygon Collider, the Box Collider works normally, but on the Polygon Colliders it is strange. if I run, it works fine but if I stop and turn, it shows the jump animation. here is a screenshot.

    https://imgur.com/CnAMubz

    Here is my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     private Rigidbody2D myRigidbody;
    8.  
    9.     private Animator myAnimator;
    10.  
    11.     [SerializeField]
    12.     private float movementSpeed;
    13.  
    14.     private bool attack;
    15.  
    16.     private bool slide;
    17.  
    18.     private bool facingRight;
    19.  
    20.     [SerializeField]
    21.     private Transform[] groundPoints;
    22.  
    23.     [SerializeField]
    24.     private float groundRadius;
    25.  
    26.     [SerializeField]
    27.     private LayerMask whatIsGround;
    28.  
    29.     private bool isGrounded;
    30.  
    31.     private bool jump;
    32.  
    33.     [SerializeField]
    34.     private float jumpForce;
    35.  
    36.  
    37.     // Start is called before the first frame update
    38.     void Start()
    39.     {
    40.         facingRight = true;
    41.         myRigidbody = GetComponent<Rigidbody2D>();
    42.         myAnimator = GetComponent<Animator>();
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         HandleInput();
    48.     }
    49.  
    50.     // Update is called once per frame
    51.     void FixedUpdate()
    52.     {
    53.         float horizontal = Input.GetAxis("Horizontal");
    54.  
    55.         isGrounded = IsGrounded();
    56.  
    57.         HandleMovement(horizontal);
    58.  
    59.         Flip(horizontal);
    60.  
    61.         HandleAttacks();
    62.  
    63.         HandleLayers();
    64.  
    65.         ResetValues();
    66.     }
    67.  
    68.     private void HandleMovement(float horizontal)
    69.     {
    70.         if (myRigidbody.velocity.y < 0)
    71.         {
    72.             myAnimator.SetBool("land", true);
    73.         }
    74.         if (!myAnimator.GetBool("slide") && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
    75.         {
    76.             myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);
    77.         }
    78.         if (isGrounded && jump)
    79.         {
    80.             isGrounded = false;
    81.             myRigidbody.AddForce(new Vector2(0, jumpForce));
    82.             myAnimator.SetTrigger("jump");
    83.         }
    84.         if (slide && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
    85.         {
    86.             myAnimator.SetBool("slide", true);
    87.         }
    88.         else if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
    89.         {
    90.             myAnimator.SetBool("slide", false);
    91.         }
    92.  
    93.         myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
    94.     }
    95.  
    96.     private void HandleAttacks()
    97.     {
    98.         if (attack && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
    99.         {
    100.             myAnimator.SetTrigger("attack");
    101.             myRigidbody.velocity = Vector2.zero;
    102.         }
    103.     }
    104.  
    105.     private void HandleInput()
    106.     {
    107.  
    108.         if (Input.GetKeyDown(KeyCode.Space))
    109.         {
    110.             jump = true;
    111.         }
    112.         if (Input.GetKey(KeyCode.Mouse0))
    113.         {
    114.             attack = true;
    115.         }
    116.         if (Input.GetKey(KeyCode.LeftShift))
    117.         {
    118.             slide = true;
    119.         }
    120.     }
    121.  
    122.     private void Flip(float horizontal)
    123.     {
    124.         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
    125.         {
    126.             facingRight = !facingRight;
    127.  
    128.             Vector3 theScale = transform.localScale;
    129.  
    130.             theScale.x *= -1;
    131.  
    132.             transform.localScale = theScale;
    133.         }
    134.     }
    135.  
    136.     private void ResetValues()
    137.     {
    138.         attack = false;
    139.         slide = false;
    140.         jump = false;
    141.     }
    142.     private bool IsGrounded()
    143.     {
    144.         if (myRigidbody.velocity.y <= 0)
    145.         {
    146.             foreach (Transform point in groundPoints)
    147.             {
    148.                 Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
    149.  
    150.                 for (int i = 0; i < colliders.Length; i++)
    151.                 {
    152.                     if (colliders[i].gameObject != gameObject)
    153.                     {
    154.                         myAnimator.ResetTrigger("jump");
    155.                         myAnimator.SetBool("land", false);
    156.                         return true;
    157.                     }
    158.                 }
    159.  
    160.                 }
    161.             }
    162.             return false;
    163.     }
    164.     private void HandleLayers()
    165.     {
    166.         if (!isGrounded)
    167.         {
    168.             myAnimator.SetLayerWeight(1, 1);
    169.         }
    170.         else
    171.         {
    172.             myAnimator.SetLayerWeight(1, 0);
    173.         }
    174.     }
    175. }
    176.  
    177.    
    178.  
    More screen shots how my setup is.

    Player

    https://imgur.com/ntdhcrJ

    Player inspector

    https://imgur.com/BV9yv5j

    Player inspector 2

    https://imgur.com/qHWwMrY

    Tiles inspector

    https://imgur.com/3ZTfnPn

    Tiles inspector 1

    https://imgur.com/hkysCzW

    Tiles inspector 2

    https://imgur.com/zxbHorP

    Tiles inspector 3
    https://imgur.com/7xC1W8T

    Rotation i tried this one to it have something to do with going up hill or something?

    https://imgur.com/MpcZNmV

    Any help wil be awsome
     
    Last edited: Aug 29, 2019