- Home /
OnCollisionEnter Logic Error
I have 2 GameObjects one is the controller and one is the enemy AI, I want to be able to detect a collision between the two, both have a Character Controller, it wont work 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 working is the OnCollisionEnter part
Do either of them have a rigidbody component? Collisions won't register unless there's a rigidbody.
Answer by TeohRIK · Dec 10, 2018 at 01:32 AM
I think it's better don't use character controller for your AI. Put something else like capsule, if your player don't have rigidbody component, you might need to put rigidbody component on your player. Then your OnCollisionEnter might work
Your answer
Follow this Question
Related Questions
OnCollisionEnter not working 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Why does OnCollisionEnter not work between a character controller and a rigidbody? 4 Answers
flying should only be possible while fuel 0 < but it continues so long as space is held 2 Answers