- Home /
Stopping objects with colliding with similar objects.
In my catapult game the player has to launch a projectile at a castle wall. The wall is made up of several bricks. These bricks are all flush to each other and do not intersect. Currently they have no rigid body attached so launching a test projectile at them would not do anything to move them. The problem comes with attaching a rigid body. Regardless of gravity, if they are not kinematic, they will collide with one another and before the projectile is launched the castle bricks will be thrown around the level. On the other hand, if they are kinematic, this means they can not be affected by the projectile, only by script.
Is there a way to tell them not to detect collisions against objects of a certain tag? Or perhaps to apply a script when the object with projectile tag has collided with them?
Currently my projectile is only fired by the motion of the catapult arm and therefore moves very slowly (increasing the motion speed would simply make the projectile clip through it. No scripts are being used to determine the momentum of the projectile but eventually there will be a calculation.
Answer by syclamoth · Jan 05, 2012 at 01:23 AM
Yes, it's easy enough to stop them from colliding with each other- just put them all on the same layer, and use Layer Based Collision to prevent that layer from colliding with itself.
However, if you do this, your entire castle will collapse as soon as the game begins! The actual problem is because of the way colliders interact with one another. First off, you should have some kind of script which automatically puts all the rigidbodies in the wall to sleep, so that they won't do anything until they are acted on by an outside force (i.e, the boulder). You may also want to increase the Physics settings' solver iteration count (this will make the simulation more stable).
Another thing- you really shouldn't be relying on physics to throw your catapult projectile. A better way to make it realistic (with minimal complicated maths on your part) would be to have the arm connected to the 'cradle' by a hinge joint, make the projectile a kinematic rigidbody that is a child of the cradle and then manually calculate the boulder's velocity one frame before you would have thrown it. Then, unchild it from the catapault, set it to non-kinematic, and manually set its velocity to the correct value! Then, you won't have to worry about the projectile falling through the catapult arm.
thanks for the advice, I had tried hinge joints before but couldn't get it working. I would love to stay away from them in all honesty. Do you know how I can set the rigidbodies to sleep? Would it be in a script? Also, how would I unchild the projectile using code? And how about setting it to non-kinematic? Sorry for the influx of questions, but your answer has left me intrigued.
All of this stuff can be done in code, yes. How much coding experience do you have? I think you'll find that it's actually impossible to make a functional game without having at least some knowledge of the systems that make it all work.
For rigidbodies, sleep them using rigidbody.Sleep. To unchild something, just use
transform.parent = null;
and setting a rigidbody to kinematic or otherwise:
rigidbody.is$$anonymous$$inematic = true;
rigidbody.is$$anonymous$$inematic = false;
I should mention that this stuff is really basic. If you don't know how to do these things already, you should work on something simpler until you are more familiar with the environment.
thanks for the help :) I am very new to unity and javascript. This is part of my second year of my games course at university and we are expected to learn from doing, hence my need for solutions to some of my problems. I'm progressing slowly with unity and can't really start a simpler project until the module is over.
Your answer
Follow this Question
Related Questions
Setting limits to my trajectory 2 Answers
Firing projectile in curve 1 Answer
How to hit gameobjects to go into the opposite direction of hit? 1 Answer
Kinematic RigidBody2D as Player Object? 0 Answers
Object gravity but no collisions 0 Answers