Skip to content

Commit

Permalink
Add speed parameter to rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
lenalinke committed Aug 11, 2024
1 parent 27fb2a3 commit 52dd5ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,32 @@ public class AgentRotationTask : AgentBaseTask, ISerializable
/// </summary>
public float Angle { get; protected set; }

public float Speed { get; protected set; }


/// <summary>
/// Create an AgentRotationTask using a target object to turn towards
/// </summary>
/// <param name="target">Target object of the rotation task</param>
public AgentRotationTask(GameObject target)
public AgentRotationTask(GameObject target, float speed = 10f)
{
Vector3 position = target.transform.position;
position.y = 0;
TargetRotation = Quaternion.LookRotation(position);
IsRotationByAngle = false;
Speed = speed;
}

/// <summary>
/// Create an AgentRotationTask using the destination coordinates
/// </summary>
/// <param name="coordinates">Coordinates of the rotation task</param>
public AgentRotationTask(Vector3 coordinates)
public AgentRotationTask(Vector3 coordinates, float speed = 10f)
{
coordinates.y = 0;
TargetRotation = Quaternion.LookRotation(coordinates);
IsRotationByAngle = false;
Speed = speed;
}

/// <summary>
Expand All @@ -56,7 +60,7 @@ public AgentRotationTask(Vector3 coordinates)
/// </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)
public AgentRotationTask(float angle, float speed= 10f, bool isRotationByAngle = true)
{
IsRotationByAngle = isRotationByAngle;
if (!isRotationByAngle)
Expand All @@ -67,6 +71,7 @@ public AgentRotationTask(float angle, bool isRotationByAngle = true)
{
Angle = angle;
}
Speed = speed;
}

/// <summary>
Expand All @@ -84,23 +89,19 @@ public override void StartExecution(Agent agent)
TargetRotation = agent.transform.rotation * Quaternion.Euler(0, Angle, 0);
}
Debug.Log("Rotation started");
agent.StartCoroutine(Rotate(agent.transform, 10f));
agent.StartCoroutine(Rotate(agent.transform));
}

private IEnumerator Rotate(Transform transform, float rotationSpeed)
private IEnumerator Rotate(Transform transform)
{
float time = 0;
while (Vector3.Distance(transform.rotation.eulerAngles, TargetRotation.eulerAngles) > 0.01f)
{
time += Time.deltaTime/rotationSpeed; //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);
//Debug.Log("Rotating...");
// Wait for the next frame
yield return null;
}
//if (transform.rotation.eulerAngles == TargetRotation.eulerAngles)
//if (Vector3.Distance(transform.rotation.eulerAngles, TargetRotation.eulerAngles) <= 0.01f)
if (Quaternion.Angle(transform.rotation, TargetRotation) <= 0.01f)
{
FinishTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,11 @@ public void ActivateOrDeactivateAdaptiveGaze(bool shouldStartOrStop, int priorit
/// Use this function to make the agent point at a target object with one arm
/// If the target is behind the agent, the agent will first rotate towards the target
/// </summary>
/// <param name="agent"></param>
/// <param name="target"></param>
/// <param name="aimLeftArm"></param>
/// <param name="aimRightArm"></param>
/// <param name="aimAtTime"></param>
/// <param name="priority"></param>
/// <param name="target">Target to point at</param>
/// <param name="aimLeftArm">True if the agent should aim with the left arm</param>
/// <param name="aimRightArm">True if the agent should aim with the right arm</param>
/// <param name="aimAtTime">How long the agent aims</param>
/// <param name="priority">Priority of the task</param>
/// <returns></returns>
public AgentBaseTask PointAt(GameObject target, bool aimLeftArm = false, bool aimRightArm = false, int aimAtTime = 5, int priority = 0)
{
Expand Down

0 comments on commit 52dd5ef

Please sign in to comment.