- Home /
Question by
Phoenix3666 · Dec 10, 2018 at 12:40 AM ·
c#charactercontrolleroncollisionenternoob
OnCollisionEnter not working
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
Comment
Best Answer
Answer by Vega4Life · Dec 10, 2018 at 12:57 AM
Make sure one of them has a rigidbody, and make sure you colliders arent set to triggers.
Thanks, thought there just had to be two colliders but ya the problem was I didn't have a rigidbody on the controller;
Your answer
Follow this Question
Related Questions
OnCollisionEnter Logic Error 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers