SHADE_Y3/Assets/Scripts/Gameplay/AIBehaviour/AIRework/RotateToVelocity.cs

91 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SHADE;
namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework
{
public class RotateToVelocity : Script
{
public float rotationPerSecond = 2.0f;
public bool active = true;
public bool rotateToPlayerLastKnown = false;
public bool lookAround = false;
private bool left = true;
private float lookOffset = 0.0f;
private float lookAroundAngle = 30.0f;
protected override void start()
{
rotateToPlayerLastKnown = false;
active = true;
}
protected override void update()
{
if (!active)
return;
RigidBody rigid = GetComponent<RigidBody>();
Transform transform = GetComponent<Transform>();
if(!lookAround)
{
lookOffset = 0.0f;
}
else
{
if(left )
{
if (lookOffset > -lookAroundAngle)
lookOffset -= rotationPerSecond * Time.DeltaTimeF;
else
left = false;
}
if (!left)
{
if (lookOffset < lookAroundAngle)
lookOffset += rotationPerSecond * Time.DeltaTimeF;
else
left = false;
}
}
if(rigid && transform)
{
Vector3 vel = rigid.LinearVelocity;
rigid.AngularVelocity = Vector3.Zero;
AILineOfSight los = GetScript<AILineOfSight>();
if(los && rotateToPlayerLastKnown)
{
Vector3 direction = los.lastFoundPos - transform.GlobalPosition;
Quaternion currentRotation = transform.LocalRotation;
Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(direction.x, direction.z) + lookOffset, 0.0f);
transform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationPerSecond * (float)Time.DeltaTimeF);
return;
}
if(vel.GetMagnitude() > 0.01f)
{
Quaternion currentRotation = transform.LocalRotation;
Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(vel.x, vel.z) + lookOffset, 0.0f);
transform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationPerSecond * (float)Time.DeltaTimeF);
}
}
}
}
}