Skip to main content
added 820 characters in body
Source Link
Kelly Thomas
  • 3.9k
  • 1
  • 24
  • 35

When facing this problem I found that I wanted the character to move forward at a velocity roughly proportional to how close they are to facing the target.

When facing away from our target we "turn on the spot" (Red), when facing directly towards it we walk straight towards it (Green), when somewhere in between we will may walk slowly while turning (Blue & Purple).

enter image description here

How best to implement this, and the values to use for thresholds, will depend on the other systems already in place.

enter image description here One approach with Unity that would allow for easy fine tuning would be to use an AnimationCurve.

public AnimationCurve speedAngleCurve;
public float maxSpeed;

private float getTargetSpeed(Vector3 targetPosition) {
    Vector3 targetDirection = targetPosition - transform.position;
    float offsetAngle = Vector3.Angle(transform.forward, targetDirection);
    float normalizedAngle = offsetAngle / 180.0f;
    return speedAngleCurve.Evaluate(normalizedAngle) * maxSpeed;
}

This would allow a developer to rapidly modify behaviour using the Inspector and the Curve Editor.

enter image description here

When facing this problem I found that I wanted the character to move forward at a velocity roughly proportional to how close they are to facing the target.

When facing away from our target we "turn on the spot" (Red), when facing directly towards it we walk straight towards it (Green), when somewhere in between we will may walk slowly while turning (Blue & Purple).

How best to implement this, and the values to use for thresholds, will depend on the other systems already in place.

enter image description here

When facing this problem I found that I wanted the character to move forward at a velocity roughly proportional to how close they are to facing the target.

When facing away from our target we "turn on the spot" (Red), when facing directly towards it we walk straight towards it (Green), when somewhere in between we will may walk slowly while turning (Blue & Purple).

enter image description here

How best to implement this, and the values to use for thresholds, will depend on the other systems already in place.

One approach with Unity that would allow for easy fine tuning would be to use an AnimationCurve.

public AnimationCurve speedAngleCurve;
public float maxSpeed;

private float getTargetSpeed(Vector3 targetPosition) {
    Vector3 targetDirection = targetPosition - transform.position;
    float offsetAngle = Vector3.Angle(transform.forward, targetDirection);
    float normalizedAngle = offsetAngle / 180.0f;
    return speedAngleCurve.Evaluate(normalizedAngle) * maxSpeed;
}

This would allow a developer to rapidly modify behaviour using the Inspector and the Curve Editor.

enter image description here

Source Link
Kelly Thomas
  • 3.9k
  • 1
  • 24
  • 35

When facing this problem I found that I wanted the character to move forward at a velocity roughly proportional to how close they are to facing the target.

When facing away from our target we "turn on the spot" (Red), when facing directly towards it we walk straight towards it (Green), when somewhere in between we will may walk slowly while turning (Blue & Purple).

How best to implement this, and the values to use for thresholds, will depend on the other systems already in place.

enter image description here