Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by code-blep · Jun 27, 2012 at 09:31 PM · collisiongravitybouncekinematic

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jun 27, 2012 at 09:42 PM 1
Share

You're trying to do a zero-gravity space shuttle sort of thing ? With objects bouncing around but the player doesn't ?

avatar image code-blep · Jun 28, 2012 at 12:25 PM 0
Share

Hey Berenger. Yes, I guess that's it really.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image code-blep · Jun 29, 2012 at 11:52 AM 0
Share

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.

avatar image Loius · Jun 29, 2012 at 03:41 PM 1
Share

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.

avatar image code-blep · Jun 29, 2012 at 04:52 PM 0
Share

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?

avatar image Loius · Jun 29, 2012 at 06:52 PM 0
Share

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

avatar image code-blep · Jun 29, 2012 at 07:18 PM 0
Share

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.

avatar image
1
Wiki

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image code-blep · Jun 29, 2012 at 06:25 PM 0
Share

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 ;)

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image code-blep · Jun 28, 2012 at 09:33 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges