so i making an object fall from the roof only when you the player get to specific amount of distance from the object
this is what i have, what can i fix?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Use this for initialization
public class FallWhenNearPlayer : MonoBehaviour
{
public Transform other;
public float killRange { get; private set; }
void Example()
{
if (other)
{
killRange = 5.0F;
float dist = Vector3.Distance(other.position, transform.position);
if (other.position - transform.position = 20)
{
GetComponent<Rigidbody>().useGravity = true;
}
}
}
}
Use OnTriggerEnter with the corresponding functionality you want the falling object to have.
I am not entirely certain here, but shouldn’t you have == in your last if, else you are assigning 20 to transform.position?
This is one method. Start by adding a RigidBody to all the objects you want to have fall. Disable the useGravity boolean so it floats.
Then add the following script to your Player:
float radius = 5; // 5 meter radius around player
void LateUpdate () {
RaycastHit[] hits = SphereCastAll ( transform.position, radius, Vector3.up, maxDistance = 0.001f );
foreach (RaycastHit hit in hits) {
try {
hit.gameObject.GetComponent<RigidBody> ().usingGravity = true;
}
catch (Exception E) {} // have to add: using System; at the top of the script
}
}