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 MartynLeeper · Dec 18, 2013 at 11:54 PM · collisionrigidbodycharactercontrollergravity

Turn Gravity on when collision occurs

Hi Everyone,

I have a FPS style game setup with cubes everywhere. When a player comes into contact with a cube a script counter begins to count down, when it reaches "0", the cubes gravity setting is turned on and force is applied to the cube. However currently the blocks are staying static.

![Here are both the Cube components (left) and the player components (right)][1] [1]: /storage/temp/19522-all.gif !

Here's the code for the player: using UnityEngine; using System.Collections;

 public class PlayerCCollision : MonoBehaviour {
 
     void OnControllerColliderHit(ControllerColliderHit hit){
         if (hit.normal.y < 0.9){ // filter out ground collisions
             // if the other object has PlayerDetect script...
             CubeFall otherScript = hit.gameObject.GetComponent<CubeFall>();
             if (otherScript){ // call its function PlayerHit
                 otherScript.PlayerHit(hit);
             }
         }
     }
 }

And the Code for the cubes:

 using UnityEngine;
 using System.Collections;
 
 public class CubeFall : MonoBehaviour {
     float timeRemaining = 10.0f;
     bool collidedWithPlayer = false;
 
 
     void Countdown()
     {
     //    Debug.Log ("Countdown called");
         timeRemaining -= Time.deltaTime;
         if (timeRemaining <= 0.0f)
         {        
             Debug.Log ("Enable Gravity");
             rigidbody.useGravity = true;
             rigidbody.AddForce (Vector3.down * 10);
         }
     }
 
     public void PlayerHit(ControllerColliderHit hit){
         Debug.Log("Player Hit");
         collidedWithPlayer = true;
     }
 
 
     void Update () {
     if (collidedWithPlayer.Equals(true))
         {
             Countdown ();
         }
     }
 
 }


So far all that happens is Debug.Log says that a "Player Hit" occurred (growing number as the game goes on) and the same with "Enabled Gravity". Can anyone tell me why please?

all.gif (438.7 kB)
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

1 Reply

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

Answer by highpockets · Dec 19, 2013 at 12:06 AM

Your rigidbody on the cubes is set to is kinematic. This means that it won't react to physics. Either set iskinematic to false in the inspector or if you want them to stay kinematic until they are hit you will just have to set the iskinematic to false with script during runtime at the same time as the gravity is set to true.

Comment
Add comment · Show 8 · 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 MartynLeeper · Dec 19, 2013 at 12:26 AM 0
Share

Thanks highpockets. The only problem is that now the counter starts and stops depending on whether or not the player is moving on the cube or not. I would like it so that if the player collides with the box at least once, the routine is run the whole way through. Do you know what I would need to do?

avatar image MartynLeeper · Dec 19, 2013 at 01:06 AM 0
Share

I took the over part of your advice and made "Is$$anonymous$$inematic" false in the inspector as well as taking out the timer code and it works is it should...except the blocks fall instantly now ins$$anonymous$$d after so many seconds. Do you think I could use the IEnumerator to wait for (say) 3 seconds before the gravity is turned on in a block?

avatar image highpockets · Dec 19, 2013 at 01:37 AM 0
Share

Yes, wether or not you set the iskinematic to false during runtime, if you use the coroutine, it should work as expected. It is the most comon way to wait for time to pass before doing something. Just be sure to use a bool or something to deter$$anonymous$$e wether or not you are waiting to fall because otherwise, you will start a new coroutine every frame the player is touching the cube. If you need help with the coroutine, I will be happy to.

avatar image highpockets · Dec 19, 2013 at 01:49 AM 0
Share

The function WaitForSeconds() is what you'll want to use

avatar image highpockets · Dec 19, 2013 at 03:35 AM 0
Share

If my answer works, mark it as accepted. This will help other users to deter$$anonymous$$e if the answer is correct and they can depend on it if they need to solve a similar problem.

Show more comments

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

19 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 avatar image

Related Questions

OnCollisionEnter does not work 4 Answers

moving at certain circumstances 1 Answer

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

CharacterController and Normal rigidbody Box Collider Collision issues 0 Answers

How to get movable/pushable cubes right? 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