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’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?