It lags when it collides. Help? :(
Hi,
I am trying (and believing) to make a game of pong. I have got a slightly decent working game. I haven't implemented the scoreboard or AI yet because I want the physics to work first. I am trying to reverse the direction of the ball when it hits either paddle just to make it work cleanly and am having the issue where it changes direction twice, it bounces off the paddle as expected and then goes in the opposite direction to when it hit the paddle -.- If anyone could help me, it would be much appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallController : MonoBehaviour {
private float randomNumber;
private float range;
private float pointOfContact;
private float[] limits;
private int speed;
private int caseNumber;
private Vector2 direction;
private float paddleTop;
private float paddleBottom;
private Rigidbody2D ballRigidbody;
// Use this for initialization
void Start () {
//Getcomponent finds the rigidbody of the attached object.
ballRigidbody = GetComponent<Rigidbody2D> ();
speed = 5;
caseNumber = Random.Range (0, 2);
range = PickARange (caseNumber);
ballDirection (speed, range);
}
void FixedUpdate() {
}
void ballDirection (int ballSpeed, float yDirection) {
randomNumber = (Random.Range (0, 100));
if (randomNumber < 49) {
direction = new Vector2(Random.Range (-1.0f, -0.5f), yDirection);
ballRigidbody.velocity = (direction * ballSpeed);
} else {
direction = new Vector2(Random.Range (0.5f, 1.0f), yDirection);
ballRigidbody.velocity = (direction * ballSpeed);
}
}
float PickARange (int number) {
switch (number) {
case(0):
range = Random.Range (0.5f, 1.0f);
return range;
case(1):
range = Random.Range (-1.0f, -0.5f);
return range;
default:
range = 1.0f;
return range;
}
}
void OnCollisionEnter2D (Collision2D coll) {
if (coll.collider.name == "Player" || coll.collider.name == "Computer") {
pointOfContact = ballRigidbody.position.y;
print (pointOfContact);
//
// paddleTop = coll.transform.position.y + 0.9f;
// print (paddleTop);
//
// paddleBottom = coll.transform.position.y - 0.9f;
// print (paddleBottom);
//
// boxCollider = coll.transform.GetComponent<BoxCollider2D> ();
// print (boxCollider.size);
//
// print (circleCollider.radius);
//
// float[] limits = {
// paddleBottom,
// paddleBottom + 0.2f,
// paddleBottom + 0.6f,
// paddleBottom + 1.2f,
// paddleBottom + 1.6f,
// paddleTop
// };
ballRigidbody.velocity = new Vector2 (ballRigidbody.velocity.x, ballRigidbody.velocity.y * -1);
}
}
}
I thought to multiply the whole velocity by -1 but that just caused a loop because of the lag. Even though that's the right way to reverse the direction. I think :p
Thanks peeps!
I can provide one but the frame rate of the video isn't good enough to really see the problem.. https://www.useloom.com/share/090f08b1274d46d081e1bba78add3712
Sorry, I would've replied sooner but I rarely get the chance to even code. I appreciate any help :)
Does the collider have a 'name' field? I know you'd use if(coll.collider == true)
to check if the Collider2D is active. To check name you use coll.gameObject.name.
Your answer
Follow this Question
Related Questions
How to move an object on a terrain that will always stay on top of the terrain? 2 Answers
How To how ?? 2 Answers
Collision Detection with DrawMeshInstanced 0 Answers
How to fix this problem? 1 Answer
Ignore collision based on position 1 Answer