Skip to content
Open
Show file tree
Hide file tree
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,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using Random = UnityEngine.Random;

namespace i5.VirtualAgents
{
Expand Down Expand Up @@ -63,7 +65,7 @@ private class AdaptiveGazeTargetInfo
/// The maximum number of targets in range that are considered for the gaze
/// </summary>
[Tooltip("The maximum number of targets in range that are considered for the gaze")]
[SerializeField] private int maxNumberOfTargetsInRange = 50;
[SerializeField] private const int maxNumberOfTargetsInRange = 50;

/// <summary>
/// The interval in seconds in which the agent looks for new targets when not moving
Expand Down Expand Up @@ -183,7 +185,7 @@ private void Start()
/// </summary>
public void Activate()
{
if (aimScript != null)
if (aimScript)
aimScript.enabled = true;

this.enabled = true;
Expand All @@ -194,7 +196,7 @@ public void Activate()
/// </summary>
public void Deactivate()
{
if (aimScript != null)
if (aimScript)
aimScript.Stop();

this.enabled = false;
Expand Down Expand Up @@ -233,7 +235,7 @@ private void AdjustIntervalBasedOnWalkingSpeed()

private void UpdatePositionOfTarget()
{
if (OverwriteGazeTarget != null)
if (OverwriteGazeTarget)
{
aimScript.SetTargetTransform(OverwriteGazeTarget);
}
Expand All @@ -246,7 +248,8 @@ private void UpdatePositionOfTarget()
aimScript.Stop();
}
}


private Collider[] colliders = new Collider[maxNumberOfTargetsInRange];
private void CheckWhichTargetsAreNearbyAndVisible()
{
timer += Time.deltaTime;
Expand All @@ -264,7 +267,7 @@ private void CheckWhichTargetsAreNearbyAndVisible()


// Check for nearby targets
Collider[] colliders = new Collider[maxNumberOfTargetsInRange];
Array.Clear(colliders, 0, colliders.Length);
// center is calculated so that corner of the bounding cube is at the position of the agent
Vector3 center = transform.position + transform.forward * Mathf.Sqrt(2 * detectionRadius * detectionRadius);

Expand All @@ -282,7 +285,7 @@ private void CheckWhichTargetsAreNearbyAndVisible()
{
AdaptiveGazeTarget target = colliders[i].GetComponent<AdaptiveGazeTarget>();
// Check that the object has an PossibleLookAtTarget component and that it is not picked up
if (target == null || !target.canCurrentlyBeLookedAt)
if (!target || !target.canCurrentlyBeLookedAt)
{
continue;
}
Expand Down Expand Up @@ -325,13 +328,8 @@ private void CheckWhichTargetsAreNearbyAndVisible()
}

// Remove targets that are no longer within the detection radius
foreach (AdaptiveGazeTargetInfo targetInfo in nearbyLookAtTargets.ToList())
{
if (targetInfo.isCurrentlyNearby == false)
{
nearbyLookAtTargets.Remove(targetInfo);
}
}
nearbyLookAtTargets.RemoveAll(targetInfo => targetInfo.isCurrentlyNearby == false);

// Calculate the most interesting target and select one by chance from the list
CalculateInterestInTargetAndSelectOne();
}
Expand Down Expand Up @@ -385,44 +383,44 @@ private AdaptiveGazeTargetInfo SelectFromListWithProbability()
// No objects available
return null;
}
else

double randomValue = Random.value;

if (randomValue <= chanceHighestRankedTarget)
{
double randomValue = Random.value;
// Select the first target
return nearbyLookAtTargets[0];
}

if (randomValue <= chanceHighestRankedTarget)
{
// Select the first target
return nearbyLookAtTargets[0];
}
else if (chanceHighestRankedTarget < randomValue && randomValue <= chanceSecondHighestTarget)
{
// Select the second target or first target when second target is not available
if (nearbyLookAtTargets.Count > 1)
return nearbyLookAtTargets[1];
else
return nearbyLookAtTargets[0];
}
else if (chanceSecondHighestTarget < randomValue && randomValue <= chanceThirdHighestTarget)
{
// Select the third target or first target when second target is not available
if (nearbyLookAtTargets.Count > 2)
return nearbyLookAtTargets[2];
else
return nearbyLookAtTargets[0];
}
else if (chanceThirdHighestTarget < randomValue && randomValue <= chanceRandomTarget)
{
// Select a random target
int randomIndex = Random.Range(0, nearbyLookAtTargets.Count);
return nearbyLookAtTargets[randomIndex];
}
else if (chanceRandomTarget < randomValue && randomValue <= chanceIdleTarget)
{
// Select no target and idle
return null;
}
if (chanceHighestRankedTarget < randomValue && randomValue <= chanceSecondHighestTarget)
{
// Select the second target or first target when second target is not available
if (nearbyLookAtTargets.Count > 1)
return nearbyLookAtTargets[1];
return nearbyLookAtTargets[0];
}

if (chanceSecondHighestTarget < randomValue && randomValue <= chanceThirdHighestTarget)
{
// Select the third target or first target when second target is not available
if (nearbyLookAtTargets.Count > 2)
return nearbyLookAtTargets[2];
return nearbyLookAtTargets[0];
}

if (chanceThirdHighestTarget < randomValue && randomValue <= chanceRandomTarget)
{
// Select a random target
int randomIndex = Random.Range(0, nearbyLookAtTargets.Count);
return nearbyLookAtTargets[randomIndex];
}

if (chanceRandomTarget < randomValue && randomValue <= chanceIdleTarget)
{
// Select no target and idle
return null;
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Serialization;

namespace i5.VirtualAgents
{
Expand All @@ -17,7 +18,7 @@ public abstract class AimAt : MonoBehaviour
[SerializeField] protected Transform targetTransform;

/// <summary>
/// The Transform of the agent childobjects that should directly aim at the target
/// The Transform of the agent childobjects that should directly aim at the target, e.g. the tip of a finger
/// </summary>
[Tooltip("The Transform of the agent childobjects that should directly aim at the target")]
[SerializeField] protected Transform aimTransform;
Expand All @@ -28,9 +29,9 @@ public abstract class AimAt : MonoBehaviour
protected AimDirection aimDirection = AimDirection.Y;

/// <summary>
/// The Transform that is acutally looked at and will follow the target smootly
/// The Position that is actually looked at and which will follow the target smoothly
/// </summary>
protected Transform targetFollower;
protected Vector3 targetFollower = Vector3.zero;

/// <summary>
/// The speed at which the agent looks at the target
Expand Down Expand Up @@ -66,10 +67,10 @@ public abstract class AimAt : MonoBehaviour
[SerializeField] protected float distanceLimit = 1.5f;

/// <summary>
/// The postion where the targetFollower should be placed when no target is set
/// The position where the targetFollower should be placed when no target is set
/// </summary>
[Tooltip("The postion where the targetFollower should be placed when no target is set")]
[SerializeField] protected Transform startingTransform;
[Tooltip("The position where the targetFollower should be placed when no target is set")]
[SerializeField] protected Vector3 startingPosition = Vector3.zero;

/// <summary>
/// The bones that should be moved to accomplish the aiming
Expand Down Expand Up @@ -122,7 +123,7 @@ protected virtual void Start()
public void SetupAndStart(Transform target, bool shouldDestroyItself = true)
{
SetBonePreset();
this.ShouldDestroyItself = shouldDestroyItself;
ShouldDestroyItself = shouldDestroyItself;
SetTargetTransform(target);
}

Expand All @@ -131,15 +132,15 @@ public void SetupAndStart(Transform target, bool shouldDestroyItself = true)
/// </summary>
public void Stop()
{
this.targetTransform = null;
targetTransform = null;
}

// LateUpdate is called once per frame, after Update
protected void LateUpdate()
{
TemporarilyIncreaseLookSpeed(navMeshAgent.velocity.magnitude);

if (targetFollower != null)
if (targetFollower != Vector3.zero)
{
UpdateTargetFollower();

Expand All @@ -161,7 +162,7 @@ protected void LateUpdate()
protected Vector3 CalculateWhereToLook()
{

Vector3 targetDirection = targetFollower.position - aimTransform.position;
Vector3 targetDirection = targetFollower - aimTransform.position;
Vector3 aimDirection = GetAimDirectionVector();
float blendOut = 0.0f;
float targetAngle = Vector3.Angle(targetDirection, aimDirection);
Expand All @@ -186,38 +187,38 @@ protected void UpdateTargetFollower()
Vector3 targetPosition;

// If targetTransform was not removed in Stop()
if (targetTransform != null)
if (targetTransform)
{
targetPosition = targetTransform.position;
increaseLookSpeedBy = 1;
}
else
{
// Return to the starting posiont
targetPosition = startingTransform.position;
// Return to the starting point
targetPosition = transform.TransformPoint(startingPosition);;


if (Vector3.Distance(targetFollower.position, targetPosition) >= 0.05f)
if (Vector3.Distance(targetFollower, targetPosition) >= 0.05f)
{
// increase LookSpeed over time to finish up the movement
increaseLookSpeedBy = Math.Min(10, increaseLookSpeedBy + 0.7f);
Weight = Math.Max(0, Weight - 0.01f);
}
else
{
targetFollower = transform.TransformPoint(startingPosition);
// When target position of the standard look is reached destroy this component
Weight = 0f;
if (ShouldDestroyItself)
{
Destroy(targetFollower.gameObject);
Destroy(this);
}
}

}

// Smooth transition to target position
targetFollower.transform.position = Vector3.Lerp(targetFollower.transform.position, targetPosition, Time.deltaTime * (currentLookSpeed * increaseLookSpeedBy));
targetFollower = Vector3.Lerp(targetFollower, targetPosition, Time.deltaTime * (currentLookSpeed * increaseLookSpeedBy));
}


Expand All @@ -232,11 +233,11 @@ protected void AimAtTarget(Transform bone, Vector3 targetPosition, float weight)

protected Vector3 GetAimDirectionVector()
{
if (this.aimDirection == AimDirection.Y)
if (aimDirection == AimDirection.Y)
return aimTransform.up.normalized;
if (this.aimDirection == AimDirection.X)
if (aimDirection == AimDirection.X)
return aimTransform.right.normalized;
if (this.aimDirection == AimDirection.Z)
if (aimDirection == AimDirection.Z)
return aimTransform.forward;

return aimTransform.up.normalized;
Expand All @@ -245,27 +246,21 @@ protected Vector3 GetAimDirectionVector()
public void SetTargetTransform(Transform targetTransform)
{
// If there is no targetFollower, create one
if (targetFollower == null)
if (targetFollower == Vector3.zero)
{
targetFollower = new GameObject().transform;
targetFollower.gameObject.name = "TargetFollower";
DebugDrawTransformSphere targetVisualizer = targetFollower.gameObject.AddComponent<DebugDrawTransformSphere>();
targetVisualizer.color = Color.red;
targetVisualizer.radius = 0.50f;

// Set starting position of targetFollower 1 unit along the current aiming direction getAimDirectionVektor() * 1f +
this.startingTransform = new GameObject().transform;
this.startingTransform.gameObject.name = "StartingPositon";
this.startingTransform.position = aimTransform.position + (GetAimDirectionVector() * 1f);
this.startingTransform.parent = this.transform;
this.targetFollower.position = startingTransform.position;
targetFollower = new Vector3();

// Set starting position of targetFollower 1 unit along the current aiming direction getAimDirectionVector() * 1f
startingPosition = new Vector3();
startingPosition = transform.InverseTransformPoint(aimTransform.position + (GetAimDirectionVector() * 1f));
targetFollower = transform.TransformPoint(startingPosition);
}

this.targetTransform = targetTransform;
}
public void TemporarilyIncreaseLookSpeed(float increase)
{
this.currentLookSpeed = LookSpeed + increase;
currentLookSpeed = LookSpeed + increase;
}

/// <summary>
Expand All @@ -290,7 +285,7 @@ public void UseNewBoneset(HumanBone[] humanBones, AimDirection aimDirection, Tra
public abstract void SetBonePreset();


protected void GetBoneTransformsFromAnimatior(HumanBodyBones aimingTip)
protected void GetBoneTransformsFromAnimator(HumanBodyBones aimingTip)
{
Animator animator = GetComponent<Animator>();
boneTransforms = new Transform[humanBones.Length];
Expand All @@ -305,10 +300,15 @@ protected void GetBoneTransformsFromAnimatior(HumanBodyBones aimingTip)
protected void OnDrawGizmos()
{
Gizmos.color = Color.green;
if (startingTransform)
if (startingPosition != Vector3.zero)
{
Gizmos.DrawWireSphere(transform.TransformPoint(startingPosition), 0.25f);
Gizmos.DrawLine(aimTransform.position, transform.TransformPoint(startingPosition));
}
Gizmos.color = Color.red;
if (targetFollower != Vector3.zero)
{
Gizmos.DrawWireSphere(startingTransform.position, 0.25f);
Gizmos.DrawLine(aimTransform.position, startingTransform.position);
Gizmos.DrawWireSphere(targetFollower, 0.25f);
}
}
}
Expand Down
Loading