Projectiles flying at inconsistent speeds

Hello, unity noob here.

I am attempting to instantiate a projectile (an arrow) at a given point, fly that arrow along the path to where my mouse is aimed, and register any hits. My approach to do so is to cast a ray at my mouse position, see if it hits anything. If it does, this is my target for my arrow shot. If it doesn’t I set a target for my arrow shot using ray.GetPoint.

The problem I am facing is that my arrows can fly at a different speed for a reason I cannot figure out.

 private void doBowShot()
    {
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray, out hit, arrowRange))
        {
            mouseWorldPosition = hit.point;
            Debug.Log("mouse hit " + hit.collider.name);
        }
        else
            mouseWorldPosition = ray.GetPoint(10);

        var aimDirection = (mouseWorldPosition - arrowPosition.transform.position).normalized;
        var arrow = Instantiate(arrowPrefab, arrowPosition.transform.position, Quaternion.LookRotation(aimDirection, Vector3.up));
        arrow.GetComponent<ArrowController>().initShot();
}
    public void initShot()
    {
        arrowRigidBody.AddForce(transform.forward * moveSpeed * Time.deltaTime);
    }

Get rid of the * Time.deltaTime part if you’re only adding the force once :slight_smile:

1 Like

Duh! Thank you.

1 Like

When you add a force, it’s time-integrated in the engine before it affects the position. What you should really use is an impulse which is an “instant force” that isn’t time-integrated by the simulation step. If you change the simulation step time right now, your projectile force and resultant initial velocity will be wrong.

Alternately, it’s much easier to just directly set the velocity of the projectile for its initial velocity.