0

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.

Notbad
  • 1,095
  • 2
  • 19
  • 31
  • remember that a thrown object has a parabolic arc – ratchet freak Apr 17 '13 at 19:54
  • Would you mind expanding a bit more the answer? I'm a bit thick right now :). – Notbad Apr 17 '13 at 19:56
  • 1
    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 http://gamedev.stackexchange.com/questions/53552/how-can-i-find-a-projectiles-launch-angle/53563#53563 might inspire you. – Patrick Hughes Apr 17 '13 at 20:17
  • 1
    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. – House Apr 17 '13 at 20:42
  • I see, thanks for all the answers. When I said gravity I meant a body attracting another. But yes, you're totally right. – Notbad Apr 17 '13 at 20:47

0 Answers0