Since I couldn’t find a guide to the latest AR game creation, so by watching old tutorials I changed the AR camera tag to the main camera and added a box collider on the camera. When a zombie comes close to the camera, the attack animation and function would be called but nothing is happening. Pic attached for reference. Kindly help
- Has the zombie a rigidbody?
- Do you use OnTriggerEnter?
- Post your code
Yes, the zombie has a rigid body and box collider. this is the code for the main camera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collisionWithCamera : MonoBehaviour
{
public bool zombieIsThere;
float timer;
int timeBetweenAttack;
public GameObject bloodyScreen;
// Use this for initialization
void Start()
{
timeBetweenAttack = 2;
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (zombieIsThere && timer >= timeBetweenAttack)
{
Attack();
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == “MainCamera”)
{
zombieIsThere = true;
}
}
void OnCollisionExit(Collision col)
{
if (col.gameObject.tag == “MainCamera”)
{
zombieIsThere = false;
}
}
void Attack()
{
timer = 0f;
GetComponent().Play(“attack”);
bloodyScreen.gameObject.SetActive(true);
StartCoroutine(wait2seconds());
}
IEnumerator wait2seconds()
{
yield return new WaitForSeconds(2f);
bloodyScreen.gameObject.SetActive(false);
}
}
posted
You use OnCollisionEnter, while the collider on the camera is a trigger, so use OnTriggerEnter