- Home /
Question by
zak666 · Dec 04, 2020 at 08:41 AM ·
scriptingbasicsonbecameinvisibleonbecamevisible
Onbecomevisable, straight up dosnt work. activates but gameobject is not in camera view?
Simple cube, simple script, but starts shooting straight away even if the Gameobject starts out of camera view. should do nothing untill its in the view but no...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunsEnemy : MonoBehaviour
{
public GameObject Projectile;
public float nextFire;
public float fireRate = 0.5f;
public Transform GunBarrelHere;
bool IsVisible;
Renderer m_Renderer;
// Use this for initialization
void Start()
{
m_Renderer = GetComponent<Renderer>();
}
void OnBecameVisible()
{
IsVisible = true;
}
void OnBecameInvisible()
{
IsVisible = false;
}
// Update is called once per frame
void LateUpdate()
{
if (IsVisible == true)
{
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(Projectile, GunBarrelHere.position, GunBarrelHere.rotation);
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
OnBecameVisible and OnBecameInvisible with LOD group. 2 Answers
Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers
why the function OnBecameVisible and OnBecameInvisible doesn't work in canvas renderer ? 1 Answer
How do I stop a moving object when the Camera sees it 1 Answer
How do I use OnBecameVisible and OnBecameInvisible? 2 Answers