1
\$\begingroup\$

I am making a 2D bow and arrow game, and I want there to be a predictive aiming line as you pull the bow back. The problem is, in my current script, it seems to only update every time I go to shoot, rather than updating as you pull the bow back. For clarity, it updates the direction, but not the increased force as you pull the bow back further.

I have put this for loop at the top of Update():

for(int i = 0; i < Points.Length; i++){
                Points[i].transform.position = PointPosition(i * time);          
            }

That calls this method:

Vector2 PointPosition(float t){
        force = new Vector2(Mathf.Clamp((startPoint.x - endPoint.x) / damp, powerMin, powerMax), Mathf.Clamp((startPoint.y - endPoint.y) / damp, powerMin, powerMax));
        Vector2 currentPointPos = (Vector2)transform.position + ((-direction.normalized * (force*mult) * t) + 0.5f * Physics2D.gravity * (t*t));
        return currentPointPos;
    }    

The predicted path is also wrong, but that is a problem for future me. For now, I'm just trying to figure out how I can get the predicted force of this line to update every second. The obvious solutions like setting force at the top of Update() didn't seem to work, and I don't know if I'm missing a Unity thing or if I've just been staring at my code for too long and can't see the obvious fix.

\$\endgroup\$
2

1 Answer 1

0
\$\begingroup\$

Meta re: your question - I'd suggest adding more code to complete your example. More context around which GameObjects you are calling Update() from could be useful for folks helping to answer your question.

Shot in the dark - If your goal is to get a bit of code to run once every second, you could probably achieve that fairly easily by keeping a running timer in your GameObject and checking it via Update():


// ...

private float timer = 0.0f;
public float timerThreshold = 0.1f;
 
// ...

void Update () {
  timer += Time.deltaTime;
 
  if (timer >= timerThreshold) {
    timer -= timerThreshold; // Reset timer
    /*
      Execute code as needed to update projectile path preview
      ...
    */
  }
}

// ...
```
\$\endgroup\$
2
  • 2
    \$\begingroup\$ One tip for better timing consistency: replace timer = 0 with timer -= timerThreshold. This way you keep any "loose change" fractions of seconds left over from overshooting your target time, and you'll trigger future timed actions correspondingly sooner. Without this, the observed time interval will tend to be longer than the value you set by about half a frame, so over a long time span you'll get fewer timed actions than you'd expect by dividing the span by the timing interval. Using -= helps it stay closer to the expected value on average, catching up a little when needed. \$\endgroup\$
    – DMGregory
    Mar 10, 2022 at 11:42
  • \$\begingroup\$ @DMGregory Great tip, edited code snippet to reflect! \$\endgroup\$
    – aransley
    Mar 16, 2022 at 19:07

You must log in to answer this question.

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