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

91 lines
2.7 KiB
C#
Raw Normal View History

2023-03-24 13:26:18 +08:00
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
{
2023-03-24 16:06:35 +08:00
public float rotationPerSecond = 2.0f;
2023-03-24 13:26:18 +08:00
2023-03-25 14:51:28 +08:00
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;
}
2023-03-24 13:26:18 +08:00
protected override void update()
{
2023-03-25 14:51:28 +08:00
if (!active)
return;
2023-03-24 13:26:18 +08:00
RigidBody rigid = GetComponent<RigidBody>();
Transform transform = GetComponent<Transform>();
2023-03-25 14:51:28 +08:00
if(!lookAround)
{
lookOffset = 0.0f;
}
2023-03-30 15:26:15 +08:00
//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;
// }
//}
2023-03-25 14:51:28 +08:00
2023-03-24 13:26:18 +08:00
if(rigid && transform)
{
Vector3 vel = rigid.LinearVelocity;
2023-03-25 14:51:28 +08:00
rigid.AngularVelocity = Vector3.Zero;
2023-03-24 13:26:18 +08:00
2023-03-25 14:51:28 +08:00
AILineOfSight los = GetScript<AILineOfSight>();
if(los && rotateToPlayerLastKnown)
2023-03-24 13:26:18 +08:00
{
2023-03-25 14:51:28 +08:00
Vector3 direction = los.lastFoundPos - transform.GlobalPosition;
2023-03-24 13:26:18 +08:00
Quaternion currentRotation = transform.LocalRotation;
2023-03-25 14:51:28 +08:00
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;
2023-03-30 15:26:15 +08:00
Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(vel.x, vel.z) + lookOffset, 0.0f);
2023-03-25 14:51:28 +08:00
transform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationPerSecond * (float)Time.DeltaTimeF);
2023-03-24 13:26:18 +08:00
}
}
}
}
}