i am looking respectfully
This commit is contained in:
parent
3160bff7a3
commit
084aada510
29 changed files with 2891 additions and 482 deletions
54
Assets/Scripts/Enemy.cs
Normal file
54
Assets/Scripts/Enemy.cs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
using Core.Extensions;
|
||||
using UnityEngine;
|
||||
|
||||
public class Enemy : Entity
|
||||
{
|
||||
[Header("Targetting")]
|
||||
public Entity closestTarget;
|
||||
[Header("Direction")]
|
||||
public float acceleration;
|
||||
public Transform[] possibleDirections;
|
||||
public float forwardPercent;
|
||||
public float strafePercent;
|
||||
private float xSign = -1f;
|
||||
private float ySign = 1f;
|
||||
private void Start()
|
||||
{
|
||||
if (Random.Range(0f, 2f) > 1f)
|
||||
{
|
||||
xSign = -xSign;
|
||||
ySign = -ySign;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void FixedUpdate()
|
||||
{
|
||||
if (stalled || !closestTarget)
|
||||
{
|
||||
rb.VelocityTowards(Vector2.zero.ScaleToMagnitude(speed), acceleration);
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector2 directionToTarget = (closestTarget.transform.position - transform.position).normalized;
|
||||
Vector2 directionToMove = directionToTarget;
|
||||
float currentHighestScore = float.NegativeInfinity;
|
||||
Vector2 strafeDirection = new Vector3(directionToTarget.y * ySign, directionToTarget.x * xSign);
|
||||
foreach (Transform direction in possibleDirections)
|
||||
{
|
||||
Vector3 directionToPoint = (direction.position - transform.position).normalized;
|
||||
float forwardScore = Vector3.Dot(directionToPoint, directionToTarget);
|
||||
float strafeScore = Vector3.Dot(directionToPoint, strafeDirection);
|
||||
float finalScore = (forwardScore * forwardPercent) + (strafeScore * strafePercent);
|
||||
//Debug.Log($"Forward: {forwardScore} Strafe: {strafeScore} Final: {finalScore}");
|
||||
if (finalScore > currentHighestScore)
|
||||
{
|
||||
//Debug.Log($"{finalScore} is higher than current score: {currentHighestScore}");
|
||||
currentHighestScore = finalScore;
|
||||
directionToMove = directionToPoint;
|
||||
}
|
||||
}
|
||||
rb.VelocityTowards(directionToMove.ScaleToMagnitude(speed), acceleration);
|
||||
FlipSprite(directionToMove);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue