- Home /
Ball control (pong)
I’m trying to make a pong like game & I was watching a old tutorial it’s very helpful but the video was so old things have changed from then to now basically what I’m trying to do is if the paddle is moving left or right the ball then needs to bounce in that direction of the paddle movement & if the paddle isn’t moving the ball must go straight here’s what I have but doesn’t seem to work
float ballForce = 200;
private new Rigidbody2D rigidbody2D;
void Start ()
{
rigidbody2D = GetComponent<Ridgidbody2D>();
float randomNumber = Random.Range(0, 2);
if (randomNumber <= 0.5)
{
rigidbody2D.AddForce(new Vector(ballForce, ballForce));
}
else
{
rigidbody2D.AddForce(new Vector2(-ballForce, -ballForce));
}
}
void OnCollisionEnter2D(Collision2D col)
{
Vector3 vel = rigidbody2D.velocity;
vel.y = rigidbody2D.velocity.y / 2 + col.collider.GetComponent<Rigidbody2D>().velocity.y / 3;
rigidbody2D.velocity = vel;
}
Answer by madks13 · Aug 22, 2018 at 05:31 AM
Ok, let's see :
Punctuations, do you know them?
As far as i know, the pong game used the point of collision on the paddle to decide which angle to send back the ball at.
You are changing y axis velocity only. From the Start method, i can see there is also movement on x axis, so where is your manipulations of velocity on x axis?
Your answer
Follow this Question
Related Questions
Make an object that moves relative to the touch more natural 0 Answers
Why does this piece of code work with gravity and this one dosent 2 Answers
Rigidbody2D AddForce only working with large numbers 2 Answers
Moving Object in constant speed in one axis while be able to control the object in all axis 1 Answer
Camera moving in Unity3d Editor, but not on Android device 1 Answer