Edit: Refined question at end.
I have an enemy that spawns at the edge of my screen and makes it way in the direction of the player. Once it reaches a certain distance from the player it will begin an orbit around them.
Any suggestions on how I can make the transition from “approaching player” to “orbiting player” smooth? I’ll see if I can illustrate what I mean:
Ideally the steps would go something like this:
-
Enemy spawns
-
Set distance from player
-
Approach this distance
-
Set point in orbit (potentially this should be a set distance away from where the ‘approach’ ends)
-
Begin smooth transition to this point.
-
Once point is reached, begin orbit.
Currently the enemy will hit the orbit range and begin orbiting straight away, making it look like it hits an “invisible wall” briefly.
My thoughts are I could use something like a Bezier curve for the smooth transition, but I’m not sure how to go about getting a point on the orbit to set as an end point for the curve.
This is the code i’m currently using to orbit once the enemy reaches a certain distance from the player:
transform.RotateAround(target.transform.position, new Vector3(0,0,1), (orbitSpeed * Time.deltaTime));
Additionally, this is all in 2D.
Hope that’s enough information, I don’t necessarily need code, but more ideas on how this can be done: anything is greatly appreciated!
Edit: I’ve solved the issue of getting a point on a circle. Specifically, I divided by circle into quadrants to get a random point along the circle in a certain quadrant.
if(quadrant == 1)
{
angle = Random.Range (90, 180);
}
else if(quadrant == 2)
{
angle = Random.Range(0, 90);
}
else if(quadrant == 3)
{
angle = Random.Range(180,270);
}
else
{
angle = Random.Range(270, 360);
}
Vector3 circ = new Vector3(Mathf.Cos (angle*Mathf.Deg2Rad), Mathf.Sin (angle*Mathf.Deg2Rad), 0);
orbitPoint = target.transform.TransformPoint (circ * radius);
My question now is: With a starting position vector and a distance, how can i get a point where the start position + distance would intersect with a point on the circle? Illustration:
