Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

2D Physics - Ball bounce

Discussion in 'Physics' started by SD2020_, May 28, 2018.

  1. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Hi all,

    I am working on a game where the ball is always bouncing, but I want to have it that the bounce height is always the same not matter how close the surface is to it's starting point.

    Is there something I can do with the Physics 2D Settings inside project settings?

    Does anyone have a solution, to keep a consistent height on bounce no matter what?


    Thanks in advance,
    Scott
     
  2. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    884
    I do not know in Physics 2D but in Physics 3D the Physics Material (inside the collider) controls how much it bounces.
    so if the Physics Material has a bounciness of 1 will bounce no matter what. But also you need to take care of Bounce Combine and other colliders Physics Material initial setup. Play with the values. If at some point loos energy can be that there is a drag apply in the Rigidbody.
     
  3. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    45
    You can use AddForce for it. But first you have to make 0 velocity on your ball. Don't forget to add tag to your ground.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. // This script requires the GameObject to have a Rigidbody2D component
    6. [RequireComponent(typeof(Rigidbody2D))]
    7. public class Movement : MonoBehaviour
    8. {
    9.     private Rigidbody2D m_rb;
    10.     public float forceHeight = 500f;
    11.  
    12.     private void Start()
    13.     {
    14.         m_rb = GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     private void OnCollisionEnter2D(Collision2D collision)
    18.     {
    19.         if (collision.gameObject.CompareTag("Ground") && m_rb != null)
    20.         {
    21.             m_rb.velocity = Vector2.zero;
    22.             m_rb.AddForce(Vector2.up * forceHeight);
    23.         }
    24.     }
    25. }
    26.