Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by dsgee · May 10, 2019 at 09:41 PM · physics2dphysicsmaterial

How to set a minimum and maximum bounce on a ball

I had a Gameobject (ball) with a physics2d material that had a bounciness of 1f. Pressing Space changes the velocity so it goes down instantly and faster than normal (think pushing a basketball down with your hand). I also set a maximum height in the script and when this is reached the velocity on the y axis is set to zero so it falls and immediately bounces back.

That worked fine, problems came in when I wanted the ball to lose momentum with each bounce normally through gravity BUT have a mimimum bounce height so it is forever bouncing at at least the minimum height and pressing space will cause it to bounce higher until max and if I left it it'll lose energy and eventually get back to the minimum bounce infinitely.

I tried having two materials. one with a bounciness of 1f and the other 0.8f and im currently trying to detect when the ball is bouncing just high enough to get to minimum bounce and if this is the case switch from the 0.8f material (normalMat in the script) to the 1f material (fullBounceMat in the script) so it stays bouncing . if I manage to press space and the ball has enough energy to go past the mimimum height I want to swap back to the 0.8f material.

Im failing terribly and im sure there has to be an easier way

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BallBounce : MonoBehaviour
 {
    //Components
    Rigidbody2D rb;
    Animator anim;
    Collider2D myCollider;
    public PhysicsMaterial2D normalBounceMat, fullBounceMat;
   //Ball Speeds
   public float moveDownSpeed;
  
   //Will be added to the move down speed when player presses Space
   public float moveDownSpeedIncrease = 10f;
 
   //Storing Inspector  Speed Values for Resetting Later
   private float defaultMoveDownSpeed;
 
   //Ball Heights
   public float minHeight, maxHeight, defaultMaxHeight, currentBallHeight, previousBallHeight;
 
   private bool falling = true;
   private bool canStopYvelocity = true;
 
   //visual cue to see if detecting falling correctly
   private bool showDropColourIndicator = false;
 
 
 
 
 
 
    void Start()
    {
     //Caching Components
     rb = GetComponent<Rigidbody2D>();
     rb.sharedMaterial = fullBounceMat;
     anim = GetComponent<Animator>();
     myCollider = GetComponent<Collider2D>();
         
     //Caching Defaults
     defaultMoveDownSpeed = moveDownSpeed;
     defaultMaxHeight = maxHeight;
     currentBallHeight = transform.position.y;
     previousBallHeight = minHeight;
 
     StartCoroutine(CheckMinHeight());
    }
 
 
 
     void Update()
    {
      
      PushBallDown();
      CheckMaxHeight();
         
    }
 
      //Meant to immediately "push" the ball down faster after pressing space
      void PushBallDown()
      { 
           if (Input.GetKeyDown(KeyCode.Space))
           {
             moveDownSpeed += moveDownSpeedIncrease;
             rb.velocity = new Vector2(rb.velocity.x, - moveDownSpeed);
         }
       }
 
     void CheckMaxHeight()
     {
         if (transform.position.y >= maxHeight)
         {
             if (canStopYvelocity)
             {
                 canStopYvelocity = false;
                 //Stoping the ball from going up further than the max height
                 rb.velocity = new Vector2(rb.velocity.x, 0f);
             }
            
         }
     }
 
     IEnumerator CheckMinHeight()
     {
         while (true)
         {
             yield return new WaitForSeconds(0.1f);
             currentBallHeight = transform.position.y;
 
             if (currentBallHeight >= previousBallHeight)
             {
                 previousBallHeight = currentBallHeight;
               
                 falling = false;
 
                 if (showDropColourIndicator)
                 {
                     anim.SetBool("falling", false);
                 }
                 
             }
             else
             {
           
                 ChooseMaterial();
                 falling = true;
                 if (showDropColourIndicator)
                 {
                     anim.SetBool("falling", true);
                 }
                 
             }
         }    
        
     }
 
     void ChooseMaterial()
     {
         if (previousBallHeight <= minHeight)
         {
             myCollider.sharedMaterial = normalBounceMat;
         }
         else if (previousBallHeight > minHeight)
         {
             myCollider.sharedMaterial= fullBounceMat;
         }
     }
 
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         canStopYvelocity = true;
         moveDownSpeed = defaultMoveDownSpeed;
         previousBallHeight = transform.position.y;
     }
 
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

175 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with raycasting 2D. Probelmas com raycasting 2D. 0 Answers

How to let player hit a boxcollider just once?(2D) 1 Answer

Top-down 2D - crawling script 0 Answers

Bubble Bubble with unity 0 Answers

Physics equivelent to transform.RotateAround 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges