Проблемы с ИИ врага
Добавлено: 14 ноя 2021, 00:01
Написал скрипт с помощью гайдов на ютубе.Проблема вот в чем,враг патрулирует,все прекрасно,но когда он агрится на главного героя он конечно следует за ним,но не подходит в плотную,а соблюдает дистанцию и когда персонаж пытается подойти в нему он убегает)) Еще пытался сделать отзеркаливание спрайта,чтоб анимация его ходьбы срабатывала не только как ходьба на право,но и как ходьба в лево,но нихера не вышло и ничерта не изменилось.Может найдет кто проблему?Буду очень признателен!
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patroler : MonoBehaviour
{
public float speed;
public int positionOnPatrol;
public Transform point;
bool moveingRight;
Transform player;
public float stoppingDistance;
bool chill = false;
bool angry = false;
bool goBack = false;
bool isFacingRight = true;
public SpriteRenderer sr;
public Vector2 moveVector;
public Animator anim;
public Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if (Vector2.Distance(transform.position, point.position) < positionOnPatrol && angry == false)
{
chill = true;
}
if (Vector2.Distance(transform.position, player.position) < stoppingDistance)
{
angry = true;
chill = false;
goBack = false;
}
if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
{
goBack = true;
angry = false;
}
if (chill == true)
{
Chill();
}
else if(angry == true)
{
Angry();
}
else if(goBack == true)
{
GoBack();
}
if(transform.position.x < player.position.x && !isFacingRight)
{
Flip();
}
else if(transform.position.x > player.position.x && isFacingRight)
{
Flip();
}
}
void Chill()
{
if(transform.position.x > point.position.x + positionOnPatrol)
{
moveingRight = false;
}
else if(transform.position.x < point.position.x - positionOnPatrol)
{
moveingRight = true;
}
if (moveingRight == true)
{
transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
}
else
{
transform.position = new Vector2(transform.position.x - speed * Time.deltaTime, transform.position.y);
}
}
void Angry()
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
void GoBack()
{
transform.position = Vector2.MoveTowards(transform.position, point.position, speed * Time.deltaTime);
}
void Flip()
{
if (moveVector.x > 0)
{
sr.flipX = false;
}
else if (moveVector.x < 0)
{
sr.flipX = true;
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class Patroler : MonoBehaviour
{
public float speed;
public int positionOnPatrol;
public Transform point;
bool moveingRight;
Transform player;
public float stoppingDistance;
bool chill = false;
bool angry = false;
bool goBack = false;
bool isFacingRight = true;
public SpriteRenderer sr;
public Vector2 moveVector;
public Animator anim;
public Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if (Vector2.Distance(transform.position, point.position) < positionOnPatrol && angry == false)
{
chill = true;
}
if (Vector2.Distance(transform.position, player.position) < stoppingDistance)
{
angry = true;
chill = false;
goBack = false;
}
if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
{
goBack = true;
angry = false;
}
if (chill == true)
{
Chill();
}
else if(angry == true)
{
Angry();
}
else if(goBack == true)
{
GoBack();
}
if(transform.position.x < player.position.x && !isFacingRight)
{
Flip();
}
else if(transform.position.x > player.position.x && isFacingRight)
{
Flip();
}
}
void Chill()
{
if(transform.position.x > point.position.x + positionOnPatrol)
{
moveingRight = false;
}
else if(transform.position.x < point.position.x - positionOnPatrol)
{
moveingRight = true;
}
if (moveingRight == true)
{
transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
}
else
{
transform.position = new Vector2(transform.position.x - speed * Time.deltaTime, transform.position.y);
}
}
void Angry()
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
void GoBack()
{
transform.position = Vector2.MoveTowards(transform.position, point.position, speed * Time.deltaTime);
}
void Flip()
{
if (moveVector.x > 0)
{
sr.flipX = false;
}
else if (moveVector.x < 0)
{
sr.flipX = true;
}
}
}