Hi there guys
I need some help creating a bounce effect between 4 walls with no gravity. I have a 2d app, where the z-axis is always 0. unfortunitely I cant have rigidbodies, since my particles are going to react slightly different than normal physics behaviour. Anyway, so my question is this: If I have a box called “wall” and a sphere called “ball”, with all colliders set up, what would the code be to let the ball bounce with the correct angle from the wall (kinda like a typical flash ball bouncing between 4 walls)?
I hope this make sense…Thanks in advance
Hmm… Well you could have the ball moving forward using transform.Translate(Vector3.forward) then when it hits a wall, take the balls current rotation, subtract it from 360 and re-rotate and then have it move forward again, something like this. Now mind you that I’m really tired and this code isn’t tested.
var speed : float;
var rotating : boolean;
function Update(){
if(!rotation){
transform.Translate(Vector3.forward * Time.deltaTime * Speed);
}
}
function OnCollisionEnter(hit : Collision){
if(hit.gameObject.tag == "Wall"){
if(!rotating){
rotating = true;
transform.rotation.y = 360 - transform.rotation.y;
rotating = false;
}
}
}
Thanks Astrauk
I like your way and I think it might be better for my application. for what it’s worth, I have also tried this, which works really well and fast, but I don’t know how to calculate x and y when 2 balls collide with each other:
speedConstant = Time.deltaTime;
if (transform.position.y > 9 || transform.position.y < -9)
{
speedY *= -1;
}
else if (transform.position.x > 9 || transform.position.x < -9)
{
speedX *= -1;
}