- Home /
How can i avoid the enemy to sight me through walls?
Hi there! I managed myself to find some resources around the internet and at the end this script came out! :3 Sadly, now i have the problem that i do not know how to avoid the enemy "Detecting" the player trough, for example a cube or a wall. Any ideas? =)
... Also, if for whatever reason the user getyour411 happens to be reading this i'll be glad for you to know that i never asked for a Script!! ¬¬ If so, i wouldn't bothered mattyman174 to explain me "HOW" not give me a way of avoid detection through walls.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform player;
float distancefrom_player;
public float look_range = 20.0f;
public float agro_range= 10.0f;
public float move_speed= 5.0f;
public float damping = 6.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
distancefrom_player = Vector3.Distance (player.position, transform.position);
}
void FixedUpdate()
{
if (distancefrom_player < look_range )
{
transform.LookAt(player);
}
if (distancefrom_player < agro_range)
{
attack();
}
}
void lookAt()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
}
void attack()
{
transform.Translate (Vector3.forward * move_speed * Time.deltaTime);
}
}
Answer by GuntranHoet · Aug 16, 2015 at 09:01 AM
Use a raycast from the enemy to the player. If the raycast hit's something other then the player or nothing; the player is out of sight. :)
Just make sure the raycast doesn't collide with the enemy itself ;)
Wow xP It's the first time i dig into the "Raycast" o.O (I don't even know what it is) Would you be so cool to give me a link to the manual or api reference? =)
If you have different layers like: enemy, player, terrain, ... you can use a public Layermask variable to pick inside the inspector which layers your raycast can hit :)
@$$anonymous$$aenor, it's not personal - even this question has been asked many times. Using the keywords from your title in Google yielded many duplicate questions/vids/tuts/etc. Your last two questions on this showed no effort or work of your own. At least here you've done some work, keep it up.