Added project
This commit is contained in:
37
Platformer/Assets/Scripts/EnemyPatrol.cs
Normal file
37
Platformer/Assets/Scripts/EnemyPatrol.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
public class EnemyPatrol : MonoBehaviour {
|
||||
|
||||
[SerializeField] private GameObject leftBorder;
|
||||
[SerializeField] private GameObject rightBorder;
|
||||
[SerializeField] private float speed;
|
||||
[SerializeField] private Rigidbody2D _rigidbody;
|
||||
[SerializeField] private SpriteRenderer spriteRender;
|
||||
[SerializeField] private CollisionDamage collisionDamage;
|
||||
[SerializeField] private GroundDetection groundDetection;
|
||||
[SerializeField] private Animator animator;
|
||||
|
||||
private bool m_IsRightDirection;
|
||||
|
||||
private void Update() {
|
||||
if (groundDetection.isGrounded) {
|
||||
if (transform.position.x > rightBorder.transform.position.x || collisionDamage.direction < 0) {
|
||||
m_IsRightDirection = false;
|
||||
} else if (transform.position.x < leftBorder.transform.position.x || collisionDamage.direction > 0) {
|
||||
m_IsRightDirection = true;
|
||||
}
|
||||
_rigidbody.velocity = m_IsRightDirection ? Vector2.right : Vector2.left;
|
||||
_rigidbody.velocity *= speed;
|
||||
}
|
||||
if (_rigidbody.velocity.x > 0) {
|
||||
spriteRender.flipX = true;
|
||||
}
|
||||
if (_rigidbody.velocity.x < 0) {
|
||||
spriteRender.flipX = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user