Oh hello, (I’m beginner with CSharp script, so do not expect something so correct).
I had watched a tutorial on how to program a monster AI to make my game, everything was going fine, but when I had the system detect the player, the trigger would not detect.
here is the monster’s AI:
public GameObject player;
public AudioClip[] footsound;
public Transform eyes;
public AudioSource music;
private NavMeshAgent nav;
private AudioSource sound;
private Animator anim;
private string state = "idle";
private bool alive = true;
// Use this for initialization
void Start ()
{
nav = GetComponent<NavMeshAgent>();
sound = GetComponent<AudioSource>();
anim = GetComponent<Animator>();
nav.speed = 1.2f;
anim.speed = 1.2f;
}
public void footstep(int _num)
{
sound.clip = footsound[_num];
sound.Play();
}
//check if he can see the player//
public void checkSight()
{
if(alive)
{
RaycastHit rayHit;
if(Physics.Linecast(eyes.position,player.transform.position, out rayHit))
{
print("hit "+rayHit.collider.gameObject.name);
if(rayHit.collider.gameObject.name == "player")
{
if(state != "kill")
{
state = "chase";
nav.speed = 3.5f;
anim.speed = 3.5f;
music.Play();
}
}
}
}
}
// Update is called once per frame
void Update ()
{
Debug.DrawLine(eyes.position, player.transform.position, Color.cyan);
if (alive)
{
anim.SetFloat("velocity", nav.velocity.magnitude);
//idle//
if(state == "idle")
{
//go to a random place//
Vector3 randompos = Random.insideUnitSphere * 30f;
NavMeshHit navHit;
NavMesh.SamplePosition(transform.position + randompos, out navHit,30f,NavMesh.AllAreas);
nav.SetDestination(navHit.position);
state = "walk";
}
//walk//
if(state == "walk")
{
if (nav.remainingDistance <= nav.stoppingDistance && !nav.pathPending)
{
state = "idle";
}
}
//chase//
if(state == "chase")
{
nav.destination = player.transform.position;
}
}
}
}
and this is the player script:
public bool alive = true;
void onTriggerEnter(Collider other)
{
if (other.gameObject.name == "eyes")
{
other.transform.parent.GetComponent<Inimigo>().checkSight();
}
}
}
Ps: I’m Brazilian, so ignore grammar mistakes because I’m using google translator :t