- Home /
Move object along ray cast using object's AddForce or velocity but not transform
In a FPS game the gun fires bullets towards the moving targets, the bullet path is calculated using Physics.Raycast(ray, out hit) method, after that I want that my cloned bullet(using Instantiate method) will follow the ray path. I found that one solution is to use Vector3.MoveTowards
void Update() {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
but if I will use transform than the bullets collapse wont be detected, that;s why I'm looking how correctly to apply AddForce to bullet, or set its velocity. I tried AddForce(hit.point) and velocity but the bullet path is not exactly as the ray path, it has slightly another rotation angles.
Answer by TonicMind · Nov 23, 2018 at 06:33 AM
Call rigodbody.AddRelativeForce(Vector3.forward * bullet force); exactly ONCE when you clone/create your bullet. The bullet must be set to face down along the z axis of your gun BEFORE adding the force.
Cheers!