I have two gameobjects, one my player one the enemy and I want to detect a Collision between them, they both have Character Controllers(Which automatically gives them colliders unless I’m mistaken) heres the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour {
//public GameObject Player1;
public Transform Player1;
int speed=0;
int range = 20;
// Use this for initialization
void Start () {
speed = 1;
}
// Update is called once per frame
void Update () {
transform.LookAt(Player1);
if(Vector3.Distance(transform.position, Player1.position)<=range)
{
transform.position += transform.forward * speed * Time.deltaTime;
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player1P")
{
print("Collision occured");
Debug.Log("Worked");
//gameObject.GetComponent<StreamVideo>().work();
}
if (col.gameObject.name == "Player1P")
{
print("Work~!");
Debug.Log("I hate this");
}
//gameObject.GetComponent<StreamVideo>().PlayVideo();
}
}
Thanks for any help - ps only part not working is OnCollisionEnter and down
Make sure one of them has a rigidbody, and make sure you colliders arent set to triggers.
Even when I added a rigidbody component and checked the colliders aren’t set to triggers, it doesn’t seem to be working…