Синтаксис:
Используется 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;
}
}
}