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

Unity2D Player passing through ground or sticking to it

Discussion in 'Scripting' started by Pedro_R, Jun 2, 2018.

  1. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    Hello,

    I've been making a platform game in which the player is like a ball that keeps bouncing in the ground and I'm currently trying to make it so when the player presses the down arrow, it does a "stomp", in other words, it's thrown with high force/velocity straight into the ground.

    Everything works perfectly until I press the button when it's low on height. In that case, the player either passes through the ground or it sticks to it and stops bouncing. I've tried to do raycasts checks to fix this, but it didn't work.

    Can someone help me?

    Player script:
    Code (CSharp):
    1. public class Player : MonoBehaviour {
    2.  
    3.     Rigidbody2D playerRB;
    4.     RaycastHit2D hit;
    5.  
    6.     public float horizontalMovement = 6.5f;
    7.     public float stompForce = 200f;
    8.     [HideInInspector] public bool isStomping = false;
    9.  
    10.     private void Awake()
    11.     {
    12.         playerRB = gameObject.GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     void Update () {
    16.         Move();
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         hit = Physics2D.Raycast(gameObject.transform.position, playerRB.velocity);
    22.         if (hit.collider != null)
    23.             gameObject.transform.position = hit.point;
    24.     }
    25.  
    26.     void Move()
    27.     {
    28.         playerRB.AddForce(Input.GetAxis("Horizontal") * horizontalMovement * Vector2.right);
    29.         if(Input.GetAxis("Vertical") < 0 && isStomping == false)
    30.         {
    31.             isStomping = true;
    32.             playerRB.velocity = Vector2.zero;
    33.             playerRB.AddForce(Vector2.down * stompForce);
    34.         }
    35.     }
    36. }
    Ground / Wall script:
    Code (CSharp):
    1. public class Wall : MonoBehaviour {
    2.  
    3.     [SerializeField] float minHeight = 1f;
    4.     [SerializeField] Vector2 damping = new Vector2(0.4f, 0.5f);
    5.     [SerializeField] float climbHeight = 0.5f;
    6.  
    7.     private float GetForceToHeight(float finalHeight)
    8.     {
    9.         return Mathf.Sqrt(2 * Physics2D.gravity.magnitude * finalHeight) / Time.fixedDeltaTime;
    10.     }
    11.  
    12.     private void OnCollisionEnter2D(Collision2D collision)
    13.     {
    14.         if (collision.gameObject.tag != "Player")
    15.             return;
    16.  
    17.         Rigidbody2D playerRB = collision.gameObject.GetComponent<Rigidbody2D>();
    18.         Vector2 normal = -collision.contacts[0].normal;
    19.         float initialHeight = Mathf.Pow(collision.contacts[0].normalImpulse, 2) / (2 * Physics2D.gravity.magnitude);
    20.  
    21.         if(normal.y != 0)
    22.         {
    23.             if (collision.gameObject.GetComponent<Player>().isStomping == true)
    24.             {
    25.                 playerRB.AddForce(normal * GetForceToHeight(minHeight * 1.5f));
    26.                 collision.gameObject.GetComponent<Player>().isStomping = false;
    27.             }
    28.             else if (initialHeight > minHeight / damping.y)
    29.                 playerRB.AddForce(normal * GetForceToHeight(initialHeight * damping.y));
    30.             else
    31.                 playerRB.AddForce(normal * GetForceToHeight(minHeight));
    32.         }
    33.  
    34.         if(normal.x != 0)
    35.         {
    36.             playerRB.velocity = new Vector2(playerRB.velocity.x, 0);
    37.             playerRB.AddForce(normal * damping.x * collision.contacts[0].normalImpulse / Time.fixedDeltaTime + (Vector2.up * GetForceToHeight(climbHeight)));
    38.         }
    39.     }
    40.  
    41. }
     
  2. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    I think I got the passing through ground thing fixed, but the player still sticks to the ground... Wasn't it supposed to be launched to the air according to the ground / wall script?
     
  3. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    Can anybody help me? :c