0
\$\begingroup\$
  • Black circles are static
  • Red - player
  • Blue line - path

I'm trying to achieve a constant movement of the red dot like on the image below. For now, I have something like:

void Update(){

    target = firstCircle; // here I need to perform some calculations
    this.transform.RotateAround(target.transform.position, Vector3.forward, speed * Time.deltaTime);

}

As you can see in the code, it rotates around the upper circle now. I need to switch the target variable to the secondCircle when the player comes back to it's initial position (or is near it).

I came up with something like:

if(Mathf.Abs(Mathf.DeltaAngle(360f,this.transform.rotation.eulerAngles.z)) < 5f)
    //change target

and it works but has two issues. First - the epsilon value. If I set it to 5f the above if is true like 7 times (depending on the player's speed). Of course I could make a lock to run it only once per cycle, but there's a second problem. If the object is moving too fast, the if will be false each time, cause the speed-step is always higher than the epsilon.

enter image description here

How is this done in 'gravity' games like AngryBirds when the bullet changes it's attraction point?

\$\endgroup\$
1
  • \$\begingroup\$ Those games actually use physics, here's what you could do: gamedev.stackexchange.com/questions/140008/… with two of those "planets" and your "player", it's actually pretty easy to do this. You just need an initial speed, and no friction while it goes around the objects (I've this same system in my game and the "bullets" my ships fire can recreate the exact same situation). \$\endgroup\$ Commented May 23, 2017 at 13:41

1 Answer 1

1
\$\begingroup\$

There are two options, both involve using a velocity vector.

Option A: Place a "trigger" at the center point. When triggered, set the center of orbit based on the direction of the velocity. IE: the dot product of the vel and the vector towards circle A > 0 then set circle A as orbit center, and vice versa. For this method, velocity can be determined by comparing current point and previous point.

The big problem with this method is that it won't work for a more complicated selection of objects. If you only need to move around 2 circles it's fine, but beyond that it's not really going to work.

Option B: Actually do the gravity calculations / steering forces. Start with some initial V and add the acceleration from gravity or target seek. Naturally this will give you the most realistic results as the object will be moving in ellipses rather than perfect spheres. For this to work, you will need to start with an initial velocity.

This scales very well. Everything works as expected regardless of the number of objects.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .