- Home /
Problem with Ball Bouncing after Collision
I am Making a Circle Pong Clone, in that the ball doesn't have a Good Bouncing behaviour i want ball behaviour like in this Video https://www.youtube.com/watch?v=nFnvaThOS30 here is the Code of Ball
Here is what i have Achieved https://drive.google.com/open?id=1kJeD3OfBn6XWoFB15Lern39lZ2oX9EWZ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public Rigidbody2D rb;
public Transform centerPoint;
public float force;
public Vector2 direction;
Vector3 startPosition;
float angle;
void Start()
{
startPosition = transform.position;
rb = GetComponent<Rigidbody2D>();
Invoke("Move", 2f);
InvokeRepeating("GameOver", 1.0f, 0.8f);
}
void Move()
{
direction = new Vector2(0.5f, -1f);
rb.AddForce(direction * force);
}
void OnCollisionEnter2D(Collision2D col)
{
Vector2 norm = col.contacts[0].normal;
Bounce(norm);
}
void Bounce(Vector2 normal)
{
normal = normal.normalized;
Vector2 newDirection = Vector2.Reflect(direction, normal);
newDirection = newDirection.normalized;
rb.AddForce(newDirection * force);
direction = newDirection;
// angle = Vector2.Angle()
}
//void CalculateAngel()
//{
//}
void GameOver()
{
if (Vector3.Distance(transform.position, centerPoint.position) > 1.2f)
{
rb.Sleep();
transform.position = startPosition;
CancelInvoke("GameOver");
Invoke("Restart",0.1f);
}
}
void Restart()
{
Invoke("Move", 2f);
InvokeRepeating("GameOver", 2.0f, 0.8f);
}
}
This Script is added to Ball along with Rigidbody & Circle Collider with a PhysicsMaterial2D Added Anyhelp
If you are using rigid bodies, they should automatically bounce off each other, but I see you have code in Bounce() to compute the bounce also. I'd suggest using only one or the other.
Answer by DerperDoing · Jun 27, 2018 at 02:48 PM
@NALAYAK A small gif/video would be helpful in understanding how the ball is bouncing in your case. Never the less, make the ball kinematic then it would respond just to the code.
@DerperDoing Here is The Video $$anonymous$$an, as you can see when the Ball Collides with the Platform it don't bounces back
https://drive.google.com/open?id=1kJeD3OfBn6XWoFB15Lern39lZ2oX9EWZ
@NALAYA$$anonymous$$ I haven't tested your code and I'm a beginner(don't know much) but why use the reflect method? Can't you just AddForce( -normal * force)? I mean apply force in the opposite direction of the direction you just got? Will it not work that way?
Answer by nareshbishtasus · Jun 27, 2018 at 03:09 PM
@NALAYAK No need to write scripts... Just create a physics material in project tab by rightclicking then selecting create then select physcis material and set bounce to one... Play with other properties to get desired result then drag this material to the tab in the collider component attached to the gameobject... I hope this should work.
I've Already Don't that But i could get the $$anonymous$$ovement Behaviour like i want to achieve watch the video https://drive.google.com/open?id=1kJeD3OfBn6XWoFB15Lern39lZ2oX9EWZ
Your answer
Follow this Question
Related Questions
OnTriggerEnter AddForce 1 Answer
How to calculate a RigidBody2D's bounce from a collider? 1 Answer
Making Pong 1 Answer
Disable bouncing effect 0 Answers