Question by
Akureyti · Nov 23, 2016 at 08:49 AM ·
collisionphysicsrangeactivation
How to provide a chraracter to dissamble the parts of a bridge by range while crossing?
I had done an online tutorial that teached me how to destroy the bridge by range, but it only works if the objects are near the bridge and only at the start of the demo. If you go there walking, it doesn't work. There is the script I used to:
using UnityEngine; using System.Collections;
public class RadialRigid : MonoBehaviour {
public LayerMask activationLayer;
public float range = 5;
public float speed = 5;
private void OnEnable()
{
var colliders = Physics.OverlapSphere(transform.position, range, activationLayer.value);
foreach(Collider c in colliders)
{
var rb = c.gameObject.GetComponent<Rigidbody>();
var distance = Vector3.Distance(transform.position, c.gameObject.transform.position);
StartCoroutine(ActivateRigidBody(rb, distance / speed));
}
}
IEnumerator ActivateRigidBody(Rigidbody rb, float delay)
{
yield return new WaitForSeconds(delay);
rb.isKinematic = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Unity 2D colliders not triggering,Unity 2D collider not triggering 0 Answers
Bounce between two exact points 1 Answer
Detect Collision of non-trigger- and trigger-colliders 1 Answer
Sphere bouncing back on edges of aligned objects 0 Answers
What's the best way to get clean, smooth collision physics? 0 Answers