- Home /
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?
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.
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?
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?
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.
The function WaitForSeconds() is what you'll want to use
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.
Your answer
Follow this Question
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