ofb/Assets/Scripts/EnemyPatrol.cs

51 lines
1.4 KiB
C#
Raw Normal View History

2023-04-11 19:55:24 -07:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour {
public bool pinned = false;
public float range;
public float xLeft;
public float xRight;
public Vector2 movementVector = Vector2.right;
public float moveSpeed;
Animator animator;
void Awake() {
animator = GetComponent<Animator>();
}
2023-04-11 19:55:24 -07:00
// Start is called before the first frame update
void Start() {
xLeft = transform.position.x - range;
xRight = transform.position.x + range;
movementVector *= moveSpeed;
}
// Update is called once per frame
void Update() {
if (!pinned) {
if (transform.position.x >= xRight || transform.position.x <= xLeft) {
movementVector = -movementVector;
GetComponent<SpriteRenderer>().flipX = !GetComponent<SpriteRenderer>().flipX;
2023-04-11 19:55:24 -07:00
}
transform.position += new Vector3(movementVector.x, 0, 0) * Time.deltaTime;
}
}
public void TogglePin(bool isPinned) {
if (isPinned) {
animator.speed = 0;
} else {
animator.speed = 1;
}
}
void OnDrawGizmos() {
Gizmos.color = Color.green;
Gizmos.DrawLine(new Vector3(transform.position.x - range, transform.position.y, transform.position.z), new Vector3(transform.position.x + range, transform.position.y, transform.position.z));
}
2023-04-11 19:55:24 -07:00
}