I have a ship which approaches a nav target and I would like to have the ship circle around at a set distance instead of running into the target. I have some code down below, which is not working.
Any help greatly appreciated.
//checks if the ship has a nav target
if (_navTarget != null)
{
//navTargetAim is the vector3 of the target, transform.position is the vector3 position of the ship
float _angle = Vector3.Angle(navTargetAim - transform.position, transform.forward);
//distToTarget is the distance between the target and the ship
float distToTarget = Vector3.Distance(_navTarget.position, transform.position);
//print("Distance to other: " + distToTarget);
//if ship is within the defined distance it is supposed to keep away from the target
if (distToTarget < spacingToTarget)
{
// ---------- this code is the problem ---------------
//was going to give it a new angle - this does not work
float _spacingAngle = Vector3.Angle(navTargetAim - transform.position, transform.right);
//my attempt. Doesn't work. I assumed this would keep the ship going around, but it doesn't.
Vector3 targetOffset = new Vector3(spacingToTarget * Mathf.Cos(_spacingAngle), 0f, spacingToTarget * Mathf.Sin(_spacingAngle));
// --------------------------------------------
//steer turns the ship towards a vector3
Steer(targetOffset);
//throttle decreases the speed of the ship. In this case it slows it down for a turn.
throttleGoal = 0f;
print("Maintaining distance" + targetOffset);
}
else
{
//steers ship toward the navTarget (direct intercept).
Steer(navTargetAim);
print("Approaching target");
}