Mini-Games-Game/Assets/Scripts/EnemyMovement.cs
2024-11-24 18:01:49 +00:00

83 lines
2.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public enum MovementTypes
{
Line,
Circle,
ThroughPlayer,
Follow,
}
public class EnemyMovement : MonoBehaviour
{
[Header("Speed")]
public float speed = 2;
public float range = 4;
public MovementTypes movementType = MovementTypes.Circle;
public Transform enemy;
public Transform player;
public Vector3 currentLookDirection = new Vector3(0f, 0f, 0f);
public float timeTillNewLookDirection = 0.5f;
public Vector3 playerStartPoint;
[Header("Spawn")]
public float radius = 5f;
void Start()
{
float randomAngle = UnityEngine.Random.Range(0f, 2 * Mathf.PI);
// Calculate the point on the perimeter of the circle
Vector3 point = player.position + new Vector3(Mathf.Cos(randomAngle) * radius, 0.6f, Mathf.Sin(randomAngle) * radius);
enemy.position = point;
if (movementType == MovementTypes.ThroughPlayer)
{
enemy.LookAt(new Vector3(player.position.x, enemy.position.y, player.position.z));
enemy.Rotate(180.0f, 0.0f, 180.0f, Space.World);
}
}
// Update is called once per frame
void FixedUpdate()
{
if (movementType == MovementTypes.Line)
{
enemy.position = new Vector3(Convert.ToSingle(Math.Cos(Time.time * speed)) * range, enemy.position.y, Time.deltaTime * speed);
}
else if (movementType == MovementTypes.Circle)
{
enemy.position = new Vector3(Convert.ToSingle(Math.Cos(Time.time * speed)) * range, enemy.position.y, Convert.ToSingle(Math.Sin(Time.time * speed)) * range);
}
else if (movementType == MovementTypes.ThroughPlayer)
{
enemy.position -= Time.deltaTime * enemy.forward * speed;
}
else if (movementType == MovementTypes.Follow)
{
enemy.position -= Time.deltaTime * enemy.forward * speed;
Vector3 newlookDirection = new Vector3(player.position.x, enemy.position.y, player.position.z);
// if (timeTillNewLookDirection <= 0) {
// newlookDirection = new Vector3(player.position.x, enemy.position.y, player.position.z);
// timeTillNewLookDirection = 0.2f;
// } else {
// newlookDirection = currentLookDirection;
// }
timeTillNewLookDirection -= Time.deltaTime;
enemy.LookAt(Vector3.Slerp(currentLookDirection, newlookDirection, 0.1f / (100000 * 10000)));
enemy.Rotate(180.0f, 0.0f, 180.0f, Space.World);
currentLookDirection = newlookDirection;
}
}
}