2024-04-29 23:54:45 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEditor;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2024-11-24 01:34:56 +00:00
|
|
|
public enum MovementTypes
|
|
|
|
{
|
|
|
|
Line,
|
|
|
|
Circle,
|
|
|
|
ThroughPlayer,
|
|
|
|
Follow,
|
|
|
|
}
|
|
|
|
|
2024-04-29 23:54:45 +01:00
|
|
|
public class EnemyMovement : MonoBehaviour
|
|
|
|
{
|
|
|
|
[Header("Speed")]
|
|
|
|
public float speed = 2;
|
|
|
|
public float range = 4;
|
2024-11-24 01:34:56 +00:00
|
|
|
public MovementTypes movementType = MovementTypes.Circle;
|
2024-04-29 23:54:45 +01:00
|
|
|
|
|
|
|
public Transform enemy;
|
2024-11-24 01:34:56 +00:00
|
|
|
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;
|
2024-04-29 23:54:45 +01:00
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2024-11-24 01:34:56 +00:00
|
|
|
float randomAngle = UnityEngine.Random.Range(0f, 2 * Mathf.PI);
|
|
|
|
|
|
|
|
// Calculate the point on the perimeter of the circle
|
2024-11-24 18:01:49 +00:00
|
|
|
Vector3 point = player.position + new Vector3(Mathf.Cos(randomAngle) * radius, 0.6f, Mathf.Sin(randomAngle) * radius);
|
2024-11-24 01:34:56 +00:00
|
|
|
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);
|
|
|
|
}
|
2024-04-29 23:54:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
2024-11-24 18:01:49 +00:00
|
|
|
void FixedUpdate()
|
2024-04-29 23:54:45 +01:00
|
|
|
{
|
2024-11-24 01:34:56 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-04-29 23:54:45 +01:00
|
|
|
}
|
|
|
|
}
|