- Home /
Collisions are driving me mad! My sanity is fading fast...
A slightly dramatic start to the question, but it paints the picture....
I'm still in the very early stages of learning Unity and scripting. I would like to make a video using the Unity Engine and have a scene in mind:
The Scenario: I have created a large cube based playing area created using 6 other GameObject cubes to form the top, bottom, and 4 sides. Inside the cube are other cubes. One is the cube which I am controlling as my 'character', I am using the mouse for direction and standard FPS keys for forward, backward movement. I am able to move the character cube around inside the playing area in all 3 dimensions. The other cubes are just objects that I would like to be able to push around, also in all 3 dimensions with no gravity applied to them.
The Problem(s):
1: I am looking to have the walls, floors, etc of the playing area stop my character from passing through them. Currently when I do collide with them, my character starts to slowly bounce around inside the playing area, eventually slowing down. I would like to have this happen without any physics being applied to my character so I don't bounce of anything.
2: I would also like to be able to push around the other cubes that I added but for them to have no gravity but with the physics being applied, so that they bounce around the playing area.
I also need to do this without using the character controller as it's shape does not fit that of the cube and when I collide with other cubes at an angle, clipping occurs making it look messy and ruins the effect.
So far I have been unable to get all these working together, due to various factors:
The Factors:
To get it partially working I have used a Rigidbody on my character cube with gravity turned off. To get the movement I want (no physics so that I don't bounce of the walls etc) I set the rigid body to be Kinematic. However when I do this the collisions no longer work and I pass straight through the walls. I have researched various ways round this but with little luck. In fact it seems to be quite a common question with the general answer being use the character controller that comes with Unity. This as mentioned is not an option due to the clipping effect. I have even starting learning OnTriggerEnter and OnCollisionEnter methods to try and achieve my goal using Scripting (Java), by preventing by character cube passing through the cubes that make up the walls. I now have the collision detections working for the walls, but I am unable to get any further. If it helps here is an example of my method:
function OnCollisionEnter(wallCollision : Collision){
if(wallCollision.gameObject.name == "mcube_top"){
Debug.Log("HIT! mcube_top");
}
Also here is a list of the construction methods I have used for the game objects:
Walls: Cube(Mesh Filter) + Box Collider (No trigger)
Character Cube: Cube(Mesh Filter) + Box Collider (No trigger) + Rigid Body (No Gravity / Is Kinematic) + Movement custom character control script
I also have a camera child and another child in the Character Cube with the following setup so I can get the collision detection working (even if I have kinematic turned on):
Character Cube Child: Cube(Mesh Filter) + Box Collider (Is trigger) + Custom collision script as shown above
The Summary: Move around in three dimensions, have walls stopping my movement without the physics bounce. Move other gravity free cubes in the area. They must also be stopped by the walls. All objects are cubes. Do all this without clipping of the cube edges with other cubes.
So, am I not understanding a fundamental aspect of Unity, is it just me that thinks the whole Kinetic thing is like an evil catch 22 situation, or am I actually on the right track. If so, does anyone want to give me a nudge in the right direction please?
Thanks
Paul
P.S. I hope this makes sense. It took me over half an hour to try and describe this clearly lol!
You're trying to do a zero-gravity space shuttle sort of thing ? With objects bouncing around but the player doesn't ?
Answer by Loius · Jun 28, 2012 at 02:46 PM
Your custom movement script should handle stopping the player. Set the player's velocity to zero when they hit a wall.
Why triggers? Non-trigger, no-gravity rigidbodies will do 'exactly' what you describe. If you move the player by setting its velocity instead of altering its position, you'll also affect the environment through 'normal' physics as you move.
"Kinematic" means your code is going to handle all movement, and physics will not affect the object anymore.
Hi Vicenti,
Interesting suggestion. So if I understand right, the very fact that I am controlling my characters velocity, means I override the velocity that is being created by the physics engine when I collide with objects? If I remember correctly, it is generally advised not to do this, but is that because it will create the very effect I am looking for? Sorry to ask for confirmation, I just want to make sure I understand the 'why' before I research and try to script it.
Like Dylan said below, generally a game retains full control over the player character and just applies physics when it's desired. If you're bouncing off walls, and clearing your velocity when you hit a wall is the desired behaviour, then you should do it. :)
You're not supposed to calculate physics - that's what the game engine is for - but if you can't figure out how to automatically stop bouncing off things I don't really see an alternative.
First of all, thank you to all of you for time on this. I have been trying to take in and understand everything. All the while I have been playing around with it and discovered that if I zero the inertiaTensor I no longer bounce of walls. I am currently testing to see how it effects other stuff. Here is what I did:
GameObject.Find("serenity").rigidbody.inertiaTensor = Vector3(0, 0, 0);
Are there any pitfalls using this method?
I've never used inertiaTensor myself, but it sounds like a bounciness factor. I do recall seeing something about it recently but I don't recall whether there were issues with using it or if it was solving some other issue. :C
So far so good Vicenti! After playing with it for a few hours it seems fine (watch $$anonymous$$urphy's law kick into effect after I post this message lol!) I declare it right at the start and I can now do everything I want. I'll vote your suggestion as the answer as it set me on the right course, and our combined effort seems to have produced an answer. Thanks to you (and all) for saving my sanity.
Answer by dylanfries · Jun 29, 2012 at 12:38 PM
Usually the way characters are controlled is to shut off their "real" physics and fake it. The way to have them interact with physics objects in the world is by checking for the collision then manually adding a force push to the object you're colliding with. Been a while since I've done this but something like
(player) function OnCollisionEnter(collision : Collision) { for (var contact : ContactPoint in collision.contacts) { // use collision.contacts to get contacted objects // use rigidbody.AddForce() to push that object away from you }
I have no suggestions about fading sanity, if anyone has a solution for that please pass it along.
Thanks Dylan. I'll investigate this route if I hit a dead end with the other suggestions. It's good to be aware of the different methods and I'm learning that there is often a number of different ways to solve stuff.
I see that there are no solutions to the sanity problem. I might just turn it into a feature ;)
Answer by diddykonga · Jun 28, 2012 at 08:01 AM
It just sounds like you need to change some of the physics materials on the objects
Hi Diddukonga. I've tried that also. Used default ones and even created my own physics material and applied it to all surfaces to make sure. It seemed to have no effect at all.
Your answer
Follow this Question
Related Questions
Both Kinematic and Non-Kinematic behaviours at the same time. 1 Answer
Kinematic RigidBody2D as Player Object? 0 Answers
Object falling because of gravity and penetrating another object 2 Answers
Stopping objects with colliding with similar objects. 1 Answer
Object gravity but no collisions 0 Answers