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

Question How can I fix my pinball-style bumper?

Discussion in 'Physics' started by jbsgroup, Dec 22, 2022.

  1. jbsgroup

    jbsgroup

    Joined:
    Aug 27, 2022
    Posts:
    4
    Hi, so I'm trying to make a pinball game, and I'm having difficulty with the bumper. This is what I have now, but when the ball hits the bumper, nothing happens. This script is attached to the bumper, can anyone see what I'm doing wrong?

    Code (CSharp):
    1. public class BumperScript : MonoBehaviour
    2. {
    3.     public GameObject bumper;
    4.     public GameObject ball;
    5.     public int bumperForce = 800;
    6.  
    7.  
    8.     private void OnTriggerEnter(Collider col)
    9.     {
    10.  
    11.         Debug.Log("Ball hit");
    12.         if (col.gameObject.tag == "Ball")
    13.         {
    14.             ball.GetComponent<Rigidbody>().AddExplosionForce(bumperForce, bumper.transform.position, 10);
    15.         }
    16.     }
    17. }
    I know the numbers are probably bigger than they need to be, I just wanted to make it really obvious it worked while testing
     
    Last edited: Dec 22, 2022
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,690
    Is your bumper a trigger? otherwise OnTriggerEnter won't be called, and you should use OnCollisionEnter instead.

    Probably you don't want the bumper to be a trigger since triggers don't generate collisions, they're only "areas" used to test if an object is inside them or not. For instance, you can use triggers to start a cutscene when the player enters a room or something like that.
     
  3. jbsgroup

    jbsgroup

    Joined:
    Aug 27, 2022
    Posts:
    4
    Finally got a chance to test it out since I was away for the holidays, but that doesn't work either. The "ball hit" debug goes off on either no matter where in the function I put it and regardless if its ontriggerenter or oncollisionenter, but the ball just doesn't get pushed back at all.
     
  4. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,690
    If that’s the case, I’d suggest debugging step by step trough your code, to see whether the AddExplosionForce function is actually being called.
     
  5. jbsgroup

    jbsgroup

    Joined:
    Aug 27, 2022
    Posts:
    4
    I added a trigger to teleport the ball back to the top when it hits the bottom bar, and for some reason it works after it teleports??? but not before???
     
  6. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,690
    Triggers, colliders, OnTriggerEnter and OnColliderEnter are pretty basic and robust building blocks, so I can guarantee there's something going on with your code. However it's close to impossible to tell you exactly what's wrong without any more info/details about your code.