Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Boomerang path

Discussion in 'Scripting' started by Marscaleb, Oct 14, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    I am trying to write the code for a boomerang the character can use as a weapon, and it is giving me far more trouble than I thought it would

    First of all, I'm only working in 2D, and it is a side view, so Y is up.

    I'm not trying to create a wide arching path; for the most part I just want the boomerang to go out, hit a target point, and come straight back to the player.
    This should be easy.

    But there are problems with getting these minute details right. First of all, the boomerang shouldn't be moving at top speed the whole time; it needs to slow down gradually and then speed up as it returns. I would like (and its easy) to have a max speed, so for the most part it will be moving at its optimal speed, but when I get to that first return point there needs to be (even if its not really noticeable) slow down and speed up. Without this it looks cheap and unnatural.

    So my first plan was to simply have the boomerang, each frame, calculate the direction to reach its target, and add a little bit of velocity in that direction. However, this created problems where the boomerang could easily just loop around its target because it can't nudge the velocity enough. But when I push that nudge value up too high, the path looks stiff because there is not enough gradual motion to it.

    By the way, its target is first a set point in front of the player, and once it reaches that, the target becomes the player itself.

    I was studying some old retro games, and I noticed their patterns suggest they move each axis individually rather than compute an angle and vector, so I thought I would try that.
    I set it so each frame the boomerang subtracted the location of itself from the location of the target, and whether that was positive or negative it would either add or subtract the nudge value to the velocity. It did that for both of its axis.
    But this caused problems where sometimes the boomerang became erratic on one axis, presumably because it overshoots its destination and repeatedly overshoots itself trying to hit that target.

    I tried to reduce this by having it reduce the nudge amount proportionally to how far away it is from its target, once it was within a certain range, but this only made the erratic corrections follow a larger arc.

    I've been thinking this over and I just can't figure out how to arrange this. What sort of logic should I be following? What sort of design should use?

    I greatly welcome any other ideas I could try.
     
  2. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
  3. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I've never coded a boomerang before. I think if I was going to do this, I'd use cubic splines, and generate 4 points, first and last at the player, second halfway-ish between the player and the target, and the third at the target. Then compute a position along this path.
     
    djfunkey likes this.
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    That WOULD generate a smooth and very nice path, but how would that handle with a target that moves in real-time?

    If the player throws the boomerang and then jumps off of a cliff, I need the boomerang to follow after the player (despite how unrealistic that is) and I foresee this method causing the boomerang to suddenly jump large distances as the spline changes with the player's position.
     
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    ...Is there a way to check where a current velocity will take an object?
    Like, I could hand over the intended velocity, and then check to see if that will overshoot my goal?

    Especially if I could do this on just one axis; predict what distance the current value would bring me.
    Although I suppose that would involve predicting how long until the next update, and I'd like to avoid fixed update because it is too choppy and unpredictable.
     
  6. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Update the end position as the player moves? If the length of the curve changes because the player is moving very fast, I guess computing the next position using the velocity is going to be needed.