Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question AR camera box collider

Discussion in 'AR' started by unity_BAA6776BB42A8E3AFE82, Jun 18, 2022.

  1. unity_BAA6776BB42A8E3AFE82

    unity_BAA6776BB42A8E3AFE82

    Joined:
    Mar 27, 2022
    Posts:
    3
    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 unity 3d.JPG
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,969
    1. Has the zombie a rigidbody?
    2. Do you use OnTriggerEnter?
    3. Post your code
     
  3. unity_BAA6776BB42A8E3AFE82

    unity_BAA6776BB42A8E3AFE82

    Joined:
    Mar 27, 2022
    Posts:
    3
    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<Animator>().Play("attack");
    bloodyScreen.gameObject.SetActive(true);
    StartCoroutine(wait2seconds());
    }

    IEnumerator wait2seconds()
    {
    yield return new WaitForSeconds(2f);
    bloodyScreen.gameObject.SetActive(false);
    }

    }
     
  4. unity_BAA6776BB42A8E3AFE82

    unity_BAA6776BB42A8E3AFE82

    Joined:
    Mar 27, 2022
    Posts:
    3
    posted
     
  5. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,969
    You use OnCollisionEnter, while the collider on the camera is a trigger, so use OnTriggerEnter
     
    andyb-unity and davidmo_unity like this.