Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Unity collider touch not act on player

Discussion in '2D' started by mafortis, Sep 22, 2020.

  1. mafortis

    mafortis

    Joined:
    Sep 17, 2019
    Posts:
    6
    I have both player and ground with colliders 2D and player is supposed to stop on top of ground but instead it stops at the bottom of the ground.



    Code

    PlayerController

    Code (CSharp):
    1. using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.  
    5.     public class PlayerController : MonoBehaviour
    6.     {
    7.         public float moveSpeed;
    8.         private float moveSpeedStore;
    9.         public float speedMultiplier;
    10.         public float speedIncreateMilestone;
    11.         private float speedIncreateMilestoneStore;
    12.         private float speedMilestoneCount;
    13.         private float speedMilestoneCountStore;
    14.         public float jumpForce;
    15.         public float jumpTime;
    16.         private float jumpTimeCounter;
    17.         private bool stoppedJumping;
    18.         private bool canDoubleJump;
    19.         private Rigidbody2D myRigidbody;
    20.         public bool grounded;
    21.         public LayerMask whatIsGround;
    22.         public Transform groundCheck;
    23.         public float groundCheckRadius;
    24.         // private Collider2D myCollider;
    25.         private Animator myAnimator;
    26.         public GameManager theGameManager;
    27.         public AudioSource jumpSound;
    28.         public AudioSource deathSound;
    29.  
    30.         // Start is called before the first frame update
    31.         void Start()
    32.         {
    33.             myRigidbody = GetComponent<Rigidbody2D>();
    34.             myAnimator = GetComponent<Animator>();
    35.  
    36.             jumpTimeCounter = jumpTime;
    37.             speedMilestoneCount = speedIncreateMilestone;
    38.             moveSpeedStore = moveSpeed;
    39.             speedMilestoneCountStore = speedMilestoneCount;
    40.             speedIncreateMilestoneStore = speedIncreateMilestone;
    41.             stoppedJumping = true;
    42.         }
    43.  
    44.         // Update is called once per frame
    45.         void Update()
    46.         {
    47.             grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    48.  
    49.             if(transform.position.x > speedMilestoneCount)
    50.             {
    51.                 speedMilestoneCount += speedIncreateMilestone;
    52.                 speedIncreateMilestone = speedIncreateMilestone * speedMultiplier;
    53.                 moveSpeed = moveSpeed * speedMultiplier;
    54.             }
    55.  
    56.             myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
    57.  
    58.             if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) )
    59.             {
    60.                 if(grounded)
    61.                 {
    62.                     myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
    63.                     stoppedJumping = false;
    64.                     jumpSound.Play();
    65.                 }
    66.  
    67.                 if(!grounded && canDoubleJump)
    68.                 {
    69.                     myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
    70.                     jumpTimeCounter = jumpTime;
    71.                     stoppedJumping = false;
    72.                     canDoubleJump = false;
    73.                     jumpSound.Play();
    74.                 }
    75.             }
    76.  
    77.             if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
    78.             {
    79.                 if(jumpTimeCounter > 0)
    80.                 {
    81.                     myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
    82.                     jumpTimeCounter -= Time.deltaTime;
    83.                 }
    84.             }
    85.  
    86.             if(Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
    87.             {
    88.                 jumpTimeCounter = 0;
    89.                 stoppedJumping = true;
    90.             }
    91.  
    92.             if(grounded)
    93.             {
    94.                 jumpTimeCounter = jumpTime;
    95.                 canDoubleJump = true;
    96.             }
    97.  
    98.             myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
    99.             myAnimator.SetBool("Grounded", grounded);
    100.         }
    101.  
    102.         void OnCollisionEnter2D(Collision2D other) {
    103.             if(other.gameObject.tag == "killbox")
    104.             {
    105.                 theGameManager.RestartGame();
    106.                 moveSpeed = moveSpeedStore;
    107.                 speedMilestoneCount = speedMilestoneCountStore;
    108.                 speedIncreateMilestone = speedIncreateMilestoneStore;
    109.                 deathSound.Play();
    110.             }
    111.         }
    112.     }
    Player settings




    Question

    What should I do to hold my player on top of ground?

    Thanks.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    Can you see the gizmo for the ground and player? From your image your BoxCollider2D gizmo seems to show you the problem in that it's way above the player rendering so physics is working fine. Your inspector shows its offset +1m up in Y too.
     
  3. mafortis

    mafortis

    Joined:
    Sep 17, 2019
    Posts:
    6
    Hi, thanks for respond. It's my bad that I haven't mentioned that I am absolute newbie in unity and unfortunately I have no idea what that `gizmo` is or where it's placed.
    And regarding to offset Y I've also tried 0 and some negative numbers such as -0.91 none changed current behavior.

    So would you mind share your knowledge the way that a newbie like me could understand what to do? :D

    Thanks again.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    The gizmo is what is rendered in the scene view to represent the object you're looking at. Lights have icons, so does the Camera. The 2D colliders have the green outlines you see and you even point to it in your image. For the BoxCollider2D it's the box shown in your image. As you can see, the box doesn't align with your player sprite renderer (it's 1m above it) because you told it to be offset 1m up. When the box rests on the surface, your player is 1m below it, again because you set that offset.

    You say you tried zero but that doesn't seem correct. The reason your at your position is because it's correct as the BoxCollider2D is resting on the surface. I'm not sure how else to describe it.
     
  5. mafortis

    mafortis

    Joined:
    Sep 17, 2019
    Posts:
    6
    Thank for response. Please describe it this way so I can get your point:
    1) should I reposition my player in edit mode?
    2) should I make my offset -1?
    Or something else that I don't understand?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    Set offset to zero. Basically you want the box to cover your sprite renderer. I mean, didn't you set the offset to (0,1)? Begs the question why you've got it set to that.

    In the end, I'm only going on the single image of your screen you posted so I cannot tell you exactly what is going wrong. You already said you set the offset to 0 and it "didn't work" so I have no idea beyond that without more information.
     
  7. mafortis

    mafortis

    Joined:
    Sep 17, 2019
    Posts:
    6
    I think I've found what cause the issue but not sure how to fix it.

    Here is my boxCollider in play mode

    001.png

    It's set to the center of my player object while my player has pivot in bottom-center

    002.png

    Now hoe can I fix that misplaced pivot?
     
  8. mafortis

    mafortis

    Joined:
    Sep 17, 2019
    Posts:
    6
    I give up, unity isn't for me!:(