I’m trying to achieve specialized movement in a circular direction around a point. For example, the character moves from in front of the point to the right of the point while maintaining their distance from the point. After some struggling with complicated math, I got to thinking I might be able to use Vector3.RotateTowards with the angleRadiansDelta calculated by (arc length formula). Problem is that the Vector3.RotateTowards result stops short of the target vector. I tried a fixed value for angleRadiansDelta with apparent success. I’m trying to keep a constant speed so that if you’re further away from the point, it will take longer to reach your destination. Here is parts of the code I’m using. I have it so that GetRelativePosition is currentVector, GetRelativePosition.magnitued is currentDist, and transform.position = (AdjustAngle * AdjustDistance) + targetPoint. I also included a screenshot of when the character stops moving.
I would be very grateful for any help with this problem. If you have any questions, I will try my best to answer them. Hopefully, we can get it solved.
private float AdjustDistance(float currentDist)
{
float distDelta = (currentDist >= targetDist) ? fwdSpeed : backSpeed;
float moveDist = Mathf.MoveTowards(currentDist, targetDist, distDelta * Time.deltaTime);
Debug.Log("Target Distance: " + targetDist.ToString() + " Current Distance: " + currentDist.ToString() + " MoveDist: " + moveDist.ToString());
Moving.y = Mathf.Sign(relativePos.magnitude - targetDist);
return moveDist;
}
private Vector3 AdjustAngle(Vector3 currentVector, Vector3 targetVector)
{
float radius = Mathf.Round(currentVector.magnitude);
float angleDelta = ((sideSpeed * 360f) / (2f * Mathf.PI * radius)) * Mathf.Deg2Rad;
Vector3 moveVector = Vector3.Normalize(Vector3.RotateTowards(currentVector, targetVector, angleDelta * Time.deltaTime, 0f));
Debug.Log("Target Angle: " + targetAngle.ToString() + "Deg = Vector3" + targetVector.ToString());
Debug.DrawRay(targetPoint, targetVector * 4f, Color.red, 0f, false);
Debug.DrawRay(targetPoint, currentVector, Color.magenta, 0f, false);
Debug.DrawRay(targetPoint + Vector3.up, moveVector * 4f, Color.black, 0f, false);
Moving.x = Mathf.Sign(Vector3.Cross(targetVector, currentVector).y);
return moveVector;
}
private Vector3 GetRelativePosition()
{
Vector3 outputPosition = transform.position - targetPoint;
outputPosition.y = 0f;
return outputPosition;
}
