Search Unity

Trying to create a simple game of Pong but the Physics engine is trying its very best to stop me :(

Discussion in 'Physics' started by Deleted User, Mar 19, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hi guys!

    I have a problem and I just cannot solve it. I want to bounce a ball off a paddle in a game of 2D pong without the physics engine producing a collision for me, before my code in the OnCollisionEnter2D method calls, changing the direction again. I have tried OnTriggerEnter2D and no luck, it works but calculating the exact position is difficult because the point of contact isn't the same every time and the paddle can go through the ball just making it look tacky if the calculations ever work. I have tried isKinematic and Dynamic and have struggled with both, I don't really want to use forces, just the velocity of the ball.

    I do have a bounciness material added to the ball, rigidbodies on the paddles and colliders on the paddles, ball and walls if that helps. I even tried Physics.autoSimulation = false; but I don't know what this even does anymore, as much as I tried looking at the APIs and forums. If anyone can help me it would be much appreciated as I don't have anyone else I can contact for help and have been stuck on this for a week... If you need to know anymore about the other scripts I have in the game files, just send me a message on here!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallMovement : MonoBehaviour {
    6.  
    7.     private float speed = 5f;
    8.     public static int playerCount = 0;
    9.     public static int computerCount = 0;
    10.     private Rigidbody2D ballRigidbody;
    11.  
    12.     void Start () {
    13.         ballRigidbody = GetComponent<Rigidbody2D> ();
    14.         ballRigidbody.velocity = Vector2.right * speed;
    15.  
    16.         //Just trying things here
    17.         Physics.autoSimulation = false;
    18.     }
    19.  
    20.     void FixedUpdate() {
    21.         //Just trying things here
    22.         ballRigidbody = GetComponent<Rigidbody2D> ();
    23.         //circleBody = GetComponent<CircleCollider2D> ();
    24.     }
    25.  
    26.     void OnCollisionEnter2D(Collision2D collision) {
    27.  
    28.  
    29.         //Calculates the position of the ball on collision and reflects with an angle and velocity relative to the position it collided at.
    30.         if (collision.gameObject.tag == "Player") {
    31.             float proportionPosition = CalculatePosition (transform.position, collision.transform.position, collision.collider.bounds.size.y);
    32.             Vector2 direction = new Vector2 (1, proportionPosition).normalized;
    33.             ballRigidbody.velocity = direction * speed * 1.1f;
    34.         }
    35.  
    36.         //Calculates the position of the ball on collision and reflects with an angle and velocity relative to the position it collided at.
    37.         if (collision.gameObject.tag == "Computer") {
    38.             float proportionPosition = CalculatePosition (transform.position, collision.transform.position, collision.collider.bounds.size.y);
    39.             Vector2 direction = new Vector2 (-1, proportionPosition).normalized;
    40.             ballRigidbody.velocity = direction * speed * 1.1f;
    41.         }
    42.  
    43.         //Essentially resetting everytime a player scores until they only need one more goal.
    44.         if (collision.gameObject.name == "LeftBoundary") {
    45.             if (computerCount == 8) {
    46.                 Reset (ballRigidbody);
    47.             } else {
    48.                 ballRigidbody.position = new Vector3 (0, 0, 0);
    49.                 ballRigidbody.velocity = Vector2.right * speed;
    50.             }
    51.             computerCount += 1;
    52.         }
    53.  
    54.         //Essentially resetting everytime a player scores until they only need one more goal.
    55.         if (collision.gameObject.name == "RightBoundary") {
    56.             if (playerCount == 8) {
    57.                 Reset (ballRigidbody);
    58.             } else {
    59.                 ballRigidbody.position = new Vector3 (0, 0, 0);
    60.                 ballRigidbody.velocity = Vector2.right * speed;
    61.             }
    62.             playerCount += 1;
    63.         }
    64.     }
    65.  
    66.     //Calculate the percentage point at where th ball hits the paddle.
    67.     float CalculatePosition(Vector2 ballPosition, Vector2 paddlePosition, float paddleHeight) {
    68.         float y = (ballPosition.y - paddlePosition.y) / paddleHeight;
    69.         return y;
    70.     }
    71.                  
    72.     //Reset the balls position.
    73.     void Reset(Rigidbody2D ball) {
    74.         ball.position = new Vector3 (0, 0, 0);
    75.         ball.velocity = new Vector2 (0, 0);
    76.     }
    77. }
    Thanks for reading this and trying to help too!
     
    Last edited by a moderator: Mar 19, 2018
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    I assume that you mean that you don't want the Physics Engine to produce the bounce, since you use OnCollisionEnter2D (you did say that it should produce the collision, but...)

    If you don't want to use forces, then why are you using bounciness? This might interfere with your scripted bounce.

    Otherwise, the script works perfectly fine for me. For the ball, I used a CircleCollider2D and a Rigidbody2D with no gravity. It seems to bounce perfectly fine.
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    Dude, all I needed was someone like you to point out how much of an idiot I've been haha. Thank you! Do you know of a way that allows you to get the velocity before the collision occurs? Possibly instead of using FixedUpdate() so that I'm not constantly reassigning the variable value?
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Sorry, for the late answer. In your case I assume that relativeVelocity should work. Since one of the objects is stationary, the relativeVelocity ought to be the balls velocity.