- Home /
Make object fall near player?
Hello,
I have an obstacle, which is above the ground and ahead of my player, and I want to make it fall when the player gets close to it. Right now, I've attached the following script to it:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class FallCheck : MonoBehaviour {
public Transform player;
public float maxDistance = 5f;
private Rigidbody rigidbody;
void Start () {
rigidbody = GetComponent<Rigidbody> ();
//rigidbody.useGravity = false;
}
void Update(){
if (Vector3.Distance (player.position, transform.position) < maxDistance) { //transform is the object that this script is attached to
rigidbody.useGravity = true;
}
}
}
But it doesn't work...when my player enters within 5f of it, it just stays in midair. I have disabled 'use gravity' and 'isKinematic' for the obstacle beforehand.
Can someone pls help? Thanks.
Answer by Eno-Khaon · Nov 18, 2020 at 04:21 AM
Unity's Rigidbody overview
Sleeping
When a Rigidbody is moving slower than a defined minimum linear or rotational speed, the physics engine assumes it has come to a halt. When this happens, the GameObject does not move again until it receives a collision or force, and so it is set to “sleeping” mode. This optimisation means that no processor time is spent updating the Rigidbody until the next time it is “awoken” (that is, set in motion again).
In other words, chances are your object was put to sleep for inactivity. Try adding something like:
rigidbody.WakeUp();
// or...
rigidbody.AddForce(Random.insideUnitSphere * 0.1f, ForceMode.VelocityChange);
after telling it to use gravity.
Your answer
Follow this Question
Related Questions
Falling through platforms 1 Answer
Make objects randomly fall 1 Answer
Detect obstacle on path 0 Answers
Is there a way to detect from which GameObject OnMouseOver come from? 2 Answers
Detect Different Terrain Texture? 2 Answers