Just like in Mario games where once you can see the enemy when he start’s moving, the positioning of the enemies is important in this one So I’m just wondering what I can do about it
Not sure what scripts to use here, I’ll give my enemy Patrol script and mention that the camera is currently a child of the player but I’m looking to change that at some point.
//Enemy Patrol script
public bool moveRight;
public int moveSpeed;
public Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
if (moveRight)
{
rb2d.velocity = new Vector2(moveSpeed, rb2d.velocity.y);
}else {
rb2d.velocity = new Vector2(-moveSpeed, rb2d.velocity.y);
}
}
}
I don’t know if I need to show any more scripts here but if I do I’ll respond quickly and post them as soon as possible, thanks in advance
You can detect if the renderer is in view of the camera. Some sources:
However, this ‘isVisible’ is also triggered by the scene view, so make sure when running this code, you have the game view over the scene view as a tab. Otherwise the scene view will see the object in view as this is also a camera / renderer.
I used this technique to enable / disable animations and voice-overs when something came into the screen or left it.
So I’m not quite sure how to apply this, stuff just like
public EnemyPatrol enemy;
enemy = FindObjectOfType();
if (enemy.IsVisible)
or
if (enemy.renderer.IsVisible)
doesnt work and I’ve googled ways to try and get stuff like IsVisible up but whenever I try type the code IsVisible isn’t even an option. Would you be able to educate me why this isn’t coming up? I know I’m missing out something huge here. Just not sure how to find out.
I am not sure if you want to use isVisible. It sounds like you work in 2D space. Just check the distance from the camera and when it is close enough, start the enemy move script. This gives you fleixibility, how long it’s is on the screen before moving.
You could use the built-in MonoBehaviour functions OnBecameVisible and OnBecameInvisible. Keep in mind that the scene view camera still counts as a camera, so if you can see your object it will still be “visible”.