intersection test between two objects in unity
this C# script is to detect collision between two objects. let's say there is a player and a enemy, add player & enemy with collider, add the script to the enemy, and then do something..in this case, enemy will be destoyed.
using UnityEngine;
using System.Collections;
public class col : MonoBehaviour {
public float collisionRadius = 1.1f;
private GameObject _player;
void Start() {
_player = GameObject.FindGameObjectWithTag("Player");
}
void Update() {
Vector3 playerPoint = _player.collider.ClosestPointOnBounds(transform.position);
float playerRadius = Vector3.Distance(_player.transform.position, playerPoint);
if (Vector3.Distance(transform.position, _player.transform.position) <= collisionRadius + playerRadius) {
// Do something!
Debug.Log ("Hello");
GameObject.Destroy(gameObject);
}
}
}
0 comments: