- Home /
(2D) How do i create a smooth movement transition from a point to another point on an orbit?
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:
![alt text][2]
[2]: /storage/temp/46036-circlepoints.png
If I were you, I would set up an inner circle trigger collider and an outer one around the player.
The enemy runs towards the player, but when it collide with the outer trigger collider, it starts rotating around the player, still try to run to the player. When it hits the inner trigger collider, it stops running, but the rotation continues.
Answer by Mike-Geig · May 08, 2015 at 06:18 PM
If you already have your orbiting code working the way you want, you can do this to remove the "invisible wall" problem. These steps assume a correct distance from player of 10 units (substitute your own number) and that you are setting the velocity of your object:
1) begin your orbiting code when the object is not quite in range of the player, say 11 units away 2) Once the object is within some distance (again, we'll say 11 units), modify its velocity each FixedUpdate to be (I am assuming a normal speed of 1 unit a second):
1.0f * (distanceToPlayer - 10.0f); //change the 10 to be what your distance should be
3) Now, as the object transitions from 11 units to 10 units away, it will slow down while the orbit code kicks in. That will give it a nice smooth transition.
Thanks for your input!
I'm currently using transform.position to move my object, rather than velocity. I've tried to adapt your code for use in this way but still seem to be getting that 'wall' issue. I'll elaborate a bit more on my code below and maybe you can provide some insight into what I'm doing wrong.
So I begin by spawning my enemy, setting it to face a point on the orbit circle (which has a radius of 3.5 units).
Vector3 dir = orbitPoint1 - transform.position;
float lookAngle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg - 90;
transform.rotation = Quaternion.AngleAxis(lookAngle, Vector3.forward);
It then begins to head in this direction
transform.position += (orbitPoint1 - startPos) * speed * Time.deltaTime;
Once it reaches a distance of 4.5 units from the player it should begin to transition towards the point smoothly. I think what you're suggesting is I do this by beginning the orbit now, while still moving towards the player until it reaches the actual orbit circle.
Vector3 distVec = transform.position - target.transform.position;
dist = distVec.magnitude;
if(dist <= 4.5)
{
transitioning = true;
approaching = false;
}
//once transitioning is true it will call this each FixedUpdate:
transform.RotateAround(new Vector3(0,0,0), new Vector3(0,0,1), (orbitSpeed * Time.deltaTime));
Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position, transform.TransformDirection(Vector3.up));
transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
transform.position += (target.transform.position - transform.position) * speed * Time.deltaTime;
I'm pretty sure this is where the problem is, but I can't quite wrap my head around why it doesn't work. Thanks for your help so far!
Your answer
![](https://koobas.hobune.stream/wayback/20220613195055im_/https://answers.unity.com/themes/thub/images/avi.jpg)