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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

My pinball game is having a collision problem

Discussion in 'Physics' started by ThomasMcDonald0, Apr 11, 2021.

  1. ThomasMcDonald0

    ThomasMcDonald0

    Joined:
    Mar 16, 2019
    Posts:
    2
    Here's the thing, when I start the game and the ball drops it touches and collides with the paddles yet when I try to flip the paddles and push the ball it sorta fazes threw, here's the code for the flippers, and any help is appreciated :
    Code (CSharp):
    1.  using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     public class RightPaddleScript : MonoBehaviour
    5.     {
    6.         void Update()
    7.         {
    8.             if (Input.GetKeyDown(KeyCode.D))
    9.             {
    10.                 transform.Rotate(0f, 0f, -22f);
    11.             }
    12.             if (Input.GetKeyUp(KeyCode.D))
    13.             {
    14.                 transform.Rotate(0f, 0f, 22f);
    15.             }
    16.         }
    17.     }
     
    Last edited: Apr 11, 2021
  2. Parixit_Jadeja

    Parixit_Jadeja

    Joined:
    Jul 27, 2017
    Posts:
    8
    Have you attached an appropriate rigidbody and collider to your paddles and ball? If yes, maybe you should try adding a physics material with a bounce to your ball.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    You should not modify the Transform when using physics. The only things that should move in physics are Rigidbody(2D) so use their API because it's their job to update the Transform with their poses; not the other way around. Also, modifying the Transform does not update the physics immediately, it only happens when the simulation runs which by default is the fixed-update. By modifying the Transform you're going to cause an instant chang in position/rotation when the simulation runs i.e. teleport from one pose to another without moving through the space inbetween.

    For flippers which don't want to respond to forces from collision, use a Kinematic Rigidbody(2D) and call MoveRotation on it. Again, physics doesn't run per-frame unless you've set that up so you should do that during the FixedUpdate or run physics per-frame.
     
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hi,
    I have no idea of what fazes threw means, but moving the paddles at 22 degrees/frame is a seriously high speed. Have you thought of making them move at a lower speed?
     
  5. ThomasMcDonald0

    ThomasMcDonald0

    Joined:
    Mar 16, 2019
    Posts:
    2
    K thanks I was also thinking of using joints and springs
     
  6. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Where did you think to put them. Between the paddles, and the ball. Isn't that another game?