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);
}