Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Hexer · Jul 27, 2013 at 04:58 PM · c#rigidbodytriggergravityenable

How to enable gravity on gameobject when interact by player

I got a problem

I want to enable gravity on a cube, if my character walks over it. Any idea what kind of script i have to write?

I know i have to add a rigedbody to my GameObject, in this case the cube. And that i need to add some kind of trigger to let it interact between the player and the cube.

 var cube : UnityEngine.GameObject;
 {
 cube.rigidbody.useGravity = true;
 }

I don't really understand scripting yet, and i want to understand it more. So can someone make an example on how this works?(java or C# doesn't matter, prefer C#) or direct me to a site or page where i can understand this better?

-Thanks for your time to read this, i hope you can help me.

Comment
Add comment
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

3 Replies

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

Answer by Forsyth · Jul 27, 2013 at 05:32 PM

One way you could do this would be to have on your cube a rigidbody, a cube collider and another collider to be a trigger. You can move and scale the trigger collider to be just above the cube for the player to walk into.

In the inspector on the cube's rigidbody, you want 'Use Gravity' to be unticked and 'Is Kinematic' to be ticked. This way, the cube won't fall initially.

Then add a script with the following (C#) to your cube:

 void OnTriggerEnter (Collider other) 
 {
     if(other.tag == "Player")
     {
         rigidbody.useGravity = true;
         rigidbody.isKinematic = false;
     }
 }

This should make it so that once the player has moved into the trigger, the cube should now respond to gravity. Make sure to add a tag called "Player" to your player character and make sure your player character has some kind of collider attached to it.

You may want to add a small delay between the player entering the trigger and the cube falling. You can do this with a simple timer variable and a boolean. Attach this to the cube instead:

 public float delay = 1f;
 
 private bool steppedOn = false;
 private float timer = 0f;
         
 void OnTriggerEnter (Collider other) 
 {
     if(other.tag == "Player")
         steppedOn = true;
 }
     
 void Update ()
 {
     if(steppedOn)
     {
         timer += Time.deltaTime;
         if(timer > delay)
         {
             rigidbody.useGravity = true;
                rigidbody.isKinematic = false;
         }
     }
 }

Hope this helps.

Comment
Add comment · Show 2 · 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 Hexer · Jul 27, 2013 at 07:00 PM 0
Share

Thanks :) it worked. I only got one $$anonymous$$or problem. I use chunks for my map so.. when 1 block fall, all blocks falls. It is a small problem but i hope to fix it soon :) (..if i even know how to fix it)

avatar image Forsyth · Jul 28, 2013 at 12:34 AM 0
Share

Hmmm that's odd and I'm not sure exactly what's causing it. Are you using one trigger collider for multiple cubes?

You could try making one single cube with the script, rigidbody, collider and trigger collider, and make sure that it works. The trigger collider should be just to trigger that individual cube to fall and should be relatively compact.

Then drag and drop this completed working cube from the hierarchy into the assets folder to make it a prefab. You can now drag and drop the prefab from the assets folder into the scene to make prefab duplicates of the cube.

They should fall individually when the player's collider moves through their individual trigger collider, but let me know if you're still having issues.

avatar image
1

Answer by aldonaletto · Jul 27, 2013 at 05:28 PM

You could do the following: add a sphere collider to the cube (menu Component/Physics/Sphere Collider - click Add in the "Replace existing component?" dialog) and set its Is Trigger field, then set its Center/Y coordinate to 1 to place it above the cube. Add the following script to the cube:

 using UnityEngine;
 using System.Collections;
 
 public class TurnGOn : MonoBehaviour {
 
     void OnTriggerEnter(Collider other) {
         if (other.tag == "Player"){ // if player entered the trigger...
             rigidbody.useGravity = true; // turn gravity on
         }
     }
 }

Notice that the player must be tagged "Player" for this to work.

Obviously, the cube should have a rigidbody (menu Component/Physics/Rigidbody) with its Use Gravity unchecked. There's a problem, however: if something hits the cube, it will start moving and spinning around crazily. You could avoid this by using Is Kinematic instead of Use Gravity: let Use Gravity set and set Is Kinematic, then clear it in OnTriggerEnter:

         ...       
         if (other.tag == "Player"){ // if player entered the trigger...
             rigidbody.isKinematic = false; // turn rigidbody on
         }
         ...

A kinematic rigidbody doesn't react to collisions or gravity - it's much like a "turned off" rigidbody, which "turns on" when rigidbody.isKinematic is set to false.

Comment
Add comment · 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
0

Answer by tw1st3d · Jul 27, 2013 at 05:13 PM

Probably by using .AddComponent()

Comment
Add comment · 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

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

18 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

Whenever I add a rigidbody and collider to my player, it interferes with the movement script and makes it teleport 1 Answer

Issue w/ Difference between Cube and Capsule 0 Answers

rigidbody.Use gravity not working from script 0 Answers

Why is my rigidbody getting stuck in the air when using mouse movement? 1 Answer


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