Search Unity

Hill Help

Discussion in '2D' started by BookwormGames, Nov 27, 2018.

  1. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Hello! For my 2D Platformer, my player can run along the ground and jump just fine. However, when I reach a hill, instead of running along it like he would the ground, my character moves along it and behaves as if he was in the air. Is there any way I can fix this? Here is my Player Controller code. Thanks for the help!
    Code (CSharp):
    1. public float speed = 5f;
    2.     public float jumpSpeed = 8f;
    3.     private float movement = 0f;
    4.     private Rigidbody2D rigidBody;
    5.     public Transform groundCheckPoint;
    6.     public float groundCheckRadius;
    7.     public LayerMask groundLayer;
    8.     private bool isTouchingGround;
    9.     private Animator playerAnimation;
    10.     public Vector3 respawnPoint;
    11.     public LevelManager gameLevelManager;
    12.     public AudioClip Running;
    13.     private AudioSource Source;
    14.     public float volLowRange = .5f;
    15.     public float volHighRange = 1.0f;
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.         rigidBody = GetComponent<Rigidbody2D>();
    21.         playerAnimation = GetComponent<Animator>();
    22.         respawnPoint = transform.position;
    23.         gameLevelManager = FindObjectOfType<LevelManager>();
    24.         Source = GetComponent<AudioSource>();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
    31.         movement = Input.GetAxis("Horizontal");
    32.         if (movement > 0f)
    33.         {
    34.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    35.             transform.localScale = new Vector2(59.20651f, 59.20651f);
    36.             Source.PlayOneShot(Running, 1F);
    37.         }
    38.         else if (movement < 0f)
    39.         {
    40.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    41.             transform.localScale = new Vector2(-59.20651f, 59.20651f);
    42.         }
    43.         else
    44.         {
    45.             rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
    46.         }
    47.         if (Input.GetButtonDown("Jump") && isTouchingGround)
    48.         {
    49.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
    50.         }
    51.         playerAnimation.SetFloat("Speed", Mathf.Abs(rigidBody.velocity.x));
    52.         playerAnimation.SetBool("OnGround", isTouchingGround);
    53.        
    54.     }
    55.  
    56.     private void OnTriggerEnter2D(Collider2D other) {
    57.         if(other.tag == "FallDetector") {
    58.             gameLevelManager.Respawn();
    59.          
    60.         }
    61.         if(other.tag == "CheckPoint") {
    62.             respawnPoint = other.transform.position;
    63.         }
    64.     }
    65.  
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    maybe change the layer from the hill to "ground"
     
    Helladah likes this.
  3. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    All of my hills are in the Ground layer. I put a Polygon 2D Collider on them. Should I change the collider?
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I have just tested: Physics2D.OverlapCircle works with Polygon2D collider (on 18.3). Also you shouldn't change it.
    Sorry, I have no idea what can be wrong. Start the game, go on the hill, switch to the scene tab, select groundcheckpoint object and check if it touches the hill collider.
     
  5. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    I don't think it does, but I'm not too sure, I couldn't see the circle that comes up when I investigate the Groundcheck.
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try to move the groundcheckpoint on the hills collider by running game
     
  7. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Should there be any changes I have to make to the Groundcheck for Level 2? The character will run up hills just fine in Level 1, but will still move along them as if he's airborne in Level 2.