How do I define the direction and speed of a projectile separately? (JS)
I'm trying to write a script in JavaScript which uses raycasting in order to fire projectiles (paintballs in this case) at whatever object the player is aiming at. My problem is that when I fire at an object, the speed the paintball travels at is relative to how far away that object is, making it very inconsistent. I'm new to using unity so I'm not exactly sure how everything works, and I haven't been able to find an answer online! Here's the code so far.
var rayOrigin = fpsCamera.ViewportToWorldPoint(Vector3(0.5f, 0.5f, 0.0f));
var hit : RaycastHit;
if(Physics.Raycast(rayOrigin, fpsCamera.transform.forward, hit)) {
var target = hit.point;
var direction = target - paintballSpawner.position;
}
else {
var ray = Ray(rayOrigin,fpsCamera.transform.forward);
direction = ray.GetPoint(100) - paintballSpawner.position;
}
var paintball = Instantiate(paintballObject, paintballSpawner.position,
transform.rotation);
paintball.GetComponent.<Rigidbody>().AddForce(direction * 20);
Thanks in advance!
Comment