- Home /
Reflecting Objects off of Boundaries/Walls
Hi
I've got walls around my game area designed to keep my hazards in the game. However, the hazards hit the wall and either stop or slide along the wall never been reflected back into the map. The hazard has a rigidbody component (gravity disabled), and a capsule collider. It is always moving on the x and z axis.
I can't think of the best way to do it, I've looked at this forum which makes sense but I haven't been able to convert it into C# partly because I've been trying to work out how to make it work with my code. Otherwise I have no idea how to do it.
If anyone has any ideas to how this can be done I would be very appreciative.
The code I'm using to make it move around randomly.
void Start()
{
//random rotator
rigidbody.angularVelocity = Random.insideUnitSphere * tumble;
//movement
Vector3 randomDirection = new Vector3(Random.Range(-1, 2), 0.0f, Random.Range(-1, 2));
rigidbody.velocity = randomDirection * speed;
}
EDIT I'm trying to get this sort of effect. Right at the beginning of this video you see the cubes hit the boundary and effortlessly without fail bounce of it back into the game area. https://www.youtube.com/watch?v=9nLIoj6KEUM
I have messed with settings in the Bouncy Physics Material and they do seem to bounce off of the boundary but they tend to still get collected and stuck on the wall and in the corners.
Answer by prof · Jul 29, 2014 at 10:29 AM
if you want proper physics, use AddForce, instead of changing velocity
EDIT example
private Vector3 randomDirection;
public float force = 20.0f;
void Start () {
randomDirection = new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f));
//Drag is how fast object slowing down
rigidbody.drag = 0;
//ForceMode.VelocityChange will ignore mass of object and apply velocity instantly
rigidbody.AddForce(randomDirection * force, ForceMode.VelocityChange);
}
Depends. Unless you need some really specific movement, using forces is better.
I just tried the bouncy physic material and it sort of worked with it. Sometimes bothered and others it didn't. Could using velocity be the reason why? I don't think it'll let me just change it from rigidbody.velocity = randomDirection speed; to rigidbody.AddForce = randomDirection speed; ether which might be a stupid thing to say.
Answer by ironblock · Jul 29, 2014 at 01:28 PM
use a physic material there is a standard(import package > physic materials ) one called bouncy. i used that one for my pinball.
Just dragged it on and it works but it doesn't seem to work all the time. One will probably bounce off of one wall then it might not bounce off another and some probably won't even bother bouncing once. I don't know why it acts like this. What was the set up on your pinball?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Balls bounce off wall in different ways 1 Answer
How to bounce one object correctly? 1 Answer