Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections;
using System.Collections;
using UnityEngine;

namespace i5.VirtualAgents.AgentTasks
Expand All @@ -15,29 +15,42 @@ public class AgentRotationTask : AgentBaseTask, ISerializable
public Quaternion TargetRotation { get; protected set; }

/// <summary>
/// Used to determine if the agent should rotate by a specific angle or towards a specific angle.
/// Used to determine if the agent should rotate by a specific angle
/// </summary>
public bool IsRotationByAngle{ get; protected set; }
public bool IsRotationByAngle { get; protected set; }

/// <summary>
/// Used to determine if the agent should rotate towards a specific angle.
/// </summary>
public bool IsRotationTowardsAngle { get; protected set; }

/// <summary>
/// The angle the agent should rotate by or towards
/// </summary>
public float Angle { get; protected set; }

/// <summary>
/// The transform the agent should rotate towards
/// </summary>
public Transform TargetTransform { get; protected set; }

/// <summary>
/// The speed at which the agent should rotate
/// </summary>
public float Speed { get; protected set; }

/// <summary>
/// Create an AgentRotationTask using a target object to turn towards
/// The angle difference at which the task is considered finished
/// </summary>
public float AngleThreshold = 0.03f;

/// <summary>
/// Create an AgentRotationTask using a target object to turn towards, position will be evaluated when task is started
/// </summary>
/// <param name="target">Target object of the rotation task</param>
public AgentRotationTask(GameObject target, float speed = 10f)
public AgentRotationTask(GameObject target, float speed = 0.8f)
{
Vector3 position = target.transform.position;
position.y = 0;
TargetRotation = Quaternion.LookRotation(position);
TargetTransform = target.transform;
IsRotationByAngle = false;
Speed = speed;
}
Expand All @@ -46,10 +59,10 @@ public AgentRotationTask(GameObject target, float speed = 10f)
/// Create an AgentRotationTask using the destination coordinates
/// </summary>
/// <param name="coordinates">Coordinates of the rotation task</param>
public AgentRotationTask(Vector3 coordinates, float speed = 10f)
public AgentRotationTask(Vector3 coordinates, float speed = 0.8f)
{
coordinates.y = 0;
TargetRotation = Quaternion.LookRotation(coordinates);
TargetTransform = new GameObject().transform;
TargetTransform.position = coordinates;
IsRotationByAngle = false;
Speed = speed;
}
Expand All @@ -62,12 +75,13 @@ public AgentRotationTask(Vector3 coordinates, float speed = 10f)
/// </summary>
/// <param name="angle">The angle to rotate by or towards, in degrees</param>
/// <param name="isRotationByAngle">True if agent should rotate by "angle" degrees, false if the rotation value of the agent should be set to "angle"</param>
public AgentRotationTask(float angle, bool isRotationByAngle = true, float speed= 10f)
public AgentRotationTask(float angle, bool isRotationByAngle = true, float speed = 0.8f)
{
IsRotationByAngle = isRotationByAngle;
if (!isRotationByAngle)
{
TargetRotation = Quaternion.Euler(0, angle, 0);
IsRotationTowardsAngle = true;
}
else
{
Expand All @@ -85,8 +99,18 @@ public override void StartExecution(Agent agent)
{
Animator animator = agent.GetComponent<Animator>();
base.StartExecution(agent);

// For target and coordinates rotation
if (!IsRotationTowardsAngle && !IsRotationByAngle)
{
Vector3 newTargetPosition = new Vector3(TargetTransform.position.x, 0, TargetTransform.position.z);
Vector3 newAgentPosition = new Vector3(agent.transform.position.x, 0, agent.transform.position.z);
float angle = Vector3.SignedAngle(agent.transform.forward, newTargetPosition - newAgentPosition, Vector3.up);
TargetRotation = agent.transform.rotation * Quaternion.Euler(0, angle, 0);
}

//For Angle rotation
if(IsRotationByAngle)
if (IsRotationByAngle)
{
TargetRotation = agent.transform.rotation * Quaternion.Euler(0, Angle, 0);
}
Expand All @@ -96,14 +120,14 @@ public override void StartExecution(Agent agent)
private IEnumerator Rotate(Transform transform)
{
float time = 0;
while (Vector3.Distance(transform.rotation.eulerAngles, TargetRotation.eulerAngles) > 0.01f)
while (Quaternion.Angle(transform.rotation, TargetRotation) > AngleThreshold)
{
time += Time.deltaTime/Speed; //to control the speed of rotation
time += Time.deltaTime * Speed; //to control the speed of rotation
// Rotate the agent a step closer to the target
transform.rotation = Quaternion.Slerp(transform.rotation, TargetRotation, time);
yield return null;
}
if (Quaternion.Angle(transform.rotation, TargetRotation) <= 0.01f)
if (Quaternion.Angle(transform.rotation, TargetRotation) <= AngleThreshold)
{
FinishTask();
}
Expand All @@ -129,4 +153,4 @@ public void Deserialize(SerializationDataContainer serializer)
Speed = serializer.GetSerializedFloat("Speed");
}
}
}
}