I have enemies and when they die they spawn an energy ball with a given velocity. I want to move this ball to the player using "gravity" to create a curved path. I have a version of the function but it is not arriving correctly to the end point without tweaking gravity force (if gravity is low, the velocity vector is not changed fast enough to get to the target position). Has anyone done this previously? I prefer to use gravity than a function curve like a bezier.

We can assume player will be stand still. So, it does not move. Target position will remain the same.

Here the code I'm using right now, I'm using unity3d:

    float gravityMultiplier = 100;

    _gravity = (targetPosition - _t.position).normalized*gravityMultiplier;
    _gravity.z = 0;

    velocity += _gravity * Time.deltaTime;

    _t.position += velocity*Time.deltaTime;

What I'm doing here is just modifying the velocity of the object to make it be attracted by a point at targetPosition. _t is just the gameobject transform.

Thanks in advance.

  • \$\begingroup\$ remember that a thrown object has a parabolic arc \$\endgroup\$ Commented Apr 17, 2013 at 19:54
  • \$\begingroup\$ Would you mind expanding a bit more the answer? I'm a bit thick right now :). \$\endgroup\$
    – Notbad
    Commented Apr 17, 2013 at 19:56
  • 1
    \$\begingroup\$ You can either use directed acceleration to replace gravity driven by homing behavior to "naturally" guide the ball around to the target, or something from this Q&A gamedev.stackexchange.com/questions/53552/… might inspire you. \$\endgroup\$ Commented Apr 17, 2013 at 20:17
  • 1
    \$\begingroup\$ You say "gravity" but it's just a force, accelerating an object. Gravity is a force, but typically in games, it's just a downward force. You want to accelerate towards a specific target, not just down. Hence, duplicate of a homing missile. \$\endgroup\$
    – House
    Commented Apr 17, 2013 at 20:42
  • \$\begingroup\$ I see, thanks for all the answers. When I said gravity I meant a body attracting another. But yes, you're totally right. \$\endgroup\$
    – Notbad
    Commented Apr 17, 2013 at 20:47


Browse other questions tagged .