Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bouncing - Landing in the right spot!

Discussion in 'Scripting' started by john-essy, Aug 20, 2017.

  1. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    Im busy trying to get my ball to land in the exact position it should after every bounce which is the centre of the next cylinder every time it bounces from one to the other. The problem here is if i have a platform that is higher than the others then the distance travelled is longer which then means i no longer land in the exact centre of my next platform which then puts the whole flow off!

    At the minute i am using rigidbody add force in fixed update. The idea is that the ball will always move to the right and bounce on every impact it has. The problem i am having at the minute is the trajectory - I want the ball to always land at the exact centre of the next cylinder no matter it's climb or drop height!

    Any ideas?
     
  2. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,676
    If you need the ball to land dead center on the cylinders, my guess is you could "cheat" in your simulation by having a force on the cylinder draw the ball toward the center.
     
    john-essy likes this.
  3. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    Thats a good idea! I had something where it rounded the position of the X axis which worked perfectly but when it was more than 0.2 from the centre it would snap and not look nice at all.
     
  4. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Code (CSharp):
    1. Vector3 Velocity(Vector3 to, float reachTime)
    2.         {
    3.             Vector3 direction = to - transform.position;
    4.            
    5.             direction -= Vector3.up * direction.y;
    6.            
    7.             return direction / reachTime + Vector3.up * (0.5f * Physics.gravity.magnitude * reachTime);
    8.         }
    set this velocity to the rigidbody
     
  5. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    Hey Simo, This is defo heading in the direction i want it too. Problem at the minute is it is just going back and forth. I thought i would share the code i have too see how you would use your Velocity helper. There are redundant variables. Am i also right in thinking with your approach i can control the speed in which the ball is travelling?

    Code (CSharp):
    1. public Transform nextTarget;
    2.     public UnityEvent OnPopped;
    3.  
    4.     public float bounceSpeed;
    5.     public float moveSpeed;
    6.  
    7.     public bool grounded;
    8.     public float radius;
    9.  
    10.     public Rigidbody rb;
    11.  
    12.     public Vector3 movementForce;
    13.  
    14.     public void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.         rb.isKinematic = true;
    18.     }
    19.  
    20.     public void StartGame()
    21.     {
    22.         rb.isKinematic = false;
    23.     }
    24.  
    25.     public void FixedUpdate()
    26.     {
    27.         rb.velocity += Velocity(movementForce, 0.5f);
    28.     }
    29.  
    30.     Vector3 Velocity(Vector3 to, float reachTime)
    31.     {
    32.         Vector3 direction = to - transform.position;
    33.  
    34.         direction -= Vector3.up * direction.y;
    35.  
    36.         return direction / reachTime + Vector3.up * (bounceSpeed * Physics.gravity.magnitude * reachTime);
    37.     }
    38.  
    39.     public void OnCollisionEnter(Collision col)
    40.     {
    41.         grounded = true;
    42.     }
    43.  
    44.     public void OnCollisionExit(Collision col)
    45.     {
    46.         grounded = false;
    47.     }
    48.  
    49.     public void OnTriggerEnter(Collider col)
    50.     {
    51.         if (col.tag == "Death")
    52.         {
    53.             OnPopped.Invoke();
    54.             rb.isKinematic = true;
    55.         }
    56.     }
     
  6. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    hopeful likes this.
  7. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Vector3 to is the target position: in your case is the top center of next cylinder!!

    you dont need to call it, all the time, what this function do, it gives the velocity to go from current position to the target position.
     
  8. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    Ahh, I did have it like that to start with, But then came the issue of knowing what the next object is, but then if i have multiple stories, i then need multiple lists or arrays to know what is next.

    This is what im heading for.
     
  9. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    cool game :)

    try this package, I think it would be a good start
     

    Attached Files:

  10. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    You are an absolute legend Simo! How would you control the speed with this? Havent looked yet just imported. Will check out the code tomorrow.

    But again ... Thank you Simo! For taking the time to help me out fella!
     
  11. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    Hey Simo,

    I managed to check it out and it works exactly like i wanted! Thank you! When i modify the reach time the bounce is smaller which then sometimes messes it up.

    Is there any way i can control the speed in which it goes through its targets while keeping the bounce high? Main reason for this is so i can change this to increase / decrease the difficulty.
     
  12. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    you can change the gravity; or you can disable gravity from the rigidbody and use your own gravity
     
  13. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    What about speed? I want to have high paced levels but right now im not sure how to increase the speed in which it does it's magic.
     
  14. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    what you did so far is right, decreasing reachTime will increase speed;
     
  15. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    Ahhh, So is there a way to have constant bounce height then? As smaller reach time of course gives smaller bounce? Sorry for all the questions man haha. Really im trying to aim for just faster moving but same kind of bounce. Also with a smaller reach time sometimes it can overshoot?
     
  16. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    this what I said before; in that case you need to play around the gravity; to have a higher bounce
     
    john-essy likes this.
  17. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    You are an absolute legend! Works exactly how i wanted it! Thank you so much!
     
  18. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I see you've already found a solution that works for you, but I'd also like to throw out there that DOTween, a tweening library which is free on the asset store, has a tween called DOJump, which you can specify a target location, jump power for consistent heights, movement speed or duration of the jump, how many jumps to reach the target, and it works for Transform or Rigidbody. It's an extremely versatile and powerful library.

    http://dotween.demigiant.com/documentation.php
     
    hopeful likes this.
  19. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    OMG, Worst thing is i use DoTween :( I can't believe i never knew this existed haha. Thanks Jeffrey
     
  20. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    No worries, I think it's a fairly new addition :D

    Besides, it's entirely possible the tween won't look exactly the way you want it to, so it's still good to know how to build it yourself for more control.
     
    hopeful likes this.