- Home /
using A* pathfinding to go to player after raycast has seen it - c#
Hi,
In my game when the player enters the enemy raycast i want it to go towards him. I've looked around and found out that a* pathfinding is the best thing to use. However I don't how to use it in this context.
 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : MonoBehaviour {
 
     bool seenPlayer;
     RaycastHit2D seePlayer;
 
     public float newPos;
 
     Animator anim;
 
 
     void Start()
     {
         anim = GetComponent<Animator> ();
     }
 
     void Update()
     {
         //Just a debug visual representation of the Linecast, can only see this in scene view! Doesn't actually do anything!
         Debug.DrawLine(transform.position, transform.position - transform.forward * newPos, Color.magenta);
 
         //Using linecast which takes (start point, end point, layermask) so we can make it only detect objects with specified layers
         //its wrapped in an if statement, so that while the tip of the Linecast (interactCheck.position) is touching an object with layer 'Guard', the code inside executes
         if(Physics2D.Raycast(transform.position - transform.forward * newPos,transform.position, 9 << LayerMask.NameToLayer("Ground")))
         {
             //we store the collider object the Linecast hit so that we can do something with that specific object, ie. the guard
             //each time the linecast touches a new object with layer "guard", it updates 'interacted' with that specific object instance
             seenPlayer = true; //since the linecase is touching the guard and we are in range, we can now interact!
             if(seenPlayer)
             {
                 Debug.Log("worked");
             }
         }
         else
         {
             seenPlayer = false; //if the linecast is not touching a guard, we cannot interact
         }
how would I implement it in this code?
               Comment
              
 
               
              Also another problem is the raycast detects the ground as well not just the player
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                