not the bounce iam looking for
i am currently trying to make a pong game for learning purposes. my current problem is i have found codes to make a ball bounce...however the ball bounces on the puck just fine but when it brushes against the wall it will either shoot directly up or directly down opposed to more of a diagonal bounce. i still believe what iam looking for is something rather simplistic...but i just cant wrap my head around it. also iam a newbie to unity; only been using the software for a week or so sooo.... yeah please help meh public class SphereCollision : MonoBehaviour { public Rigidbody RbSphere; public float mag; public float Constantspeed; #region
#endregion
// Use this for initialization
void Start ()
{
RbSphere = GetComponent<Rigidbody>();
mag = RbSphere.position.magnitude;
Constantspeed = 1.0f;
}
void Udate()
{
}
void OnCollisionEnter(Collision CollisionInfo)
{
var direction = RbSphere.GetPointVelocity (transform.localEulerAngles);
RbSphere.AddForce(Vector3.Reflect(direction, CollisionInfo.contacts[0].normal) * mag, ForceMode.Impulse);
RbSphere.velocity = Constantspeed * (RbSphere.velocity.normalized);
}
// Update is called once per frame
}
Answer by OfficialCoatsee · Apr 09, 2017 at 06:26 AM
Just a suggestion, I'm not sure if this will work for you - but try adding the rubber material to the mesh on your walls, and see if that makes any sort of difference.
Alternatively, what you could to, is detect what exactly the ball is colliding against, before applying the bounce code.
So...
void OnCollisionEnter(Collision CollisionInfo) {
if (CollisionInfo.transform.tag == "wall") {
//use less?
} else {
var direction = RbSphere.GetPointVelocity (transform.localEulerAngles);
RbSphere.AddForce(Vector3.Reflect(direction, CollisionInfo.contacts[0].normal) * mag, ForceMode.Impulse);
RbSphere.velocity = Constantspeed * (RbSphere.velocity.normalized);
}
Let me know if any of this either helps you or doesn't.
sorry it took so long to reply but yeah i ended up realizing that i needed to delete gravity and from that point at start i added a continuous force at a random range on the x and y axis... its running pretty smoothly now, thank you sir :]
Answer by Kidroflz · Apr 20, 2017 at 12:42 AM
i deleted gravity and added a script for random range
sx = Random.Range (-10, 10) == 0 ? -1 : 1;
sy = Random.Range (-10, 10) == 0 ? -1 : 1;
Rbsphere.velocity = new Vector3(Random.Range(-15,15) * sx, Random.Range(-15,15) * sy, 0);
then after that i added that code into the re spawn method. i even eventually added the code i used above into the collision script to make the bounce //in my opinion seem more fluid.
Direction = Rbsphere.GetPointVelocity(Rbsphere.transform.localPosition);
Rbsphere.AddForce(Vector3.Reflect(Direction ,Ballcol.contacts[0].normal)*Mag, ForceMode.Impulse);
Mag being a variable that i state in the start function Mag = Rbsphere.transform.position.magnitude /13;
i divided it by a number because the results i would get from mag would make the bounce of the ball seem un-natural. hope this helps other people in the future, btw these are a combinations of codes pulled from tutorials and from the unity learn tab, i know someone wont sue me but shout out to those who helped others befor hand.
Your answer
Follow this Question
Related Questions
Raycast direction change problem 0 Answers
2D Enemy AI problems. 1 Answer
Struggling with bounce pad in platformer game. 1 Answer
Update instatiate doesn't work? 1 Answer
Why does my Pong paddle code work? 1 Answer