I'm trying to make a roguelike styled game (to put you in context). I'm trying to make a script that makes the enemies follows the main character but stops when there is a wall between.
So I'm trying to make this with Raycasting, if the ray detects a wall between the enemie and the character, the enemie won't move.
Well, the results for now are horrible.
The code I have is something like this:
// Start is called before the first frame update
void Start()
{
followCharacter = false;
}
// Update is called once per frame
void Update()
{
if (followCharacter == true)
{
FollowCharacter();
RayCast();
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if(collision.tag == "Character")
{
followCharacter = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == "Character")
{
followCharacter = false ;
}
}
private void FollowCharacter()
{//Just a Vector2.MoveTowards Script
controlador.transform.position = Vector2.MoveTowards(controlador.transform.position, controladorPersonaje.transform.position, speed * Time.deltaTime);
}
private void RayCast()
{
hit = Physics2D.Raycast(enemieTransform.position, character.transform.position); //that character
//is a game
//object
Debug.DrawRay(transformPropio.position, character.transform.position *10f, Color.yellow);
if (hit.collider.gameObject.tag == "Wall")
{
followCharacter = false;
}
}
I think there must be something wrong in the logic im trying to follow. I want the Ray of the Raycast to always follow the character.
I must clarify that the Collider that is cheking the collisions is a circle around the enemie.
Thanks for all!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…