- Home /
Problem in Shooting Accuracy
Hello everyone i got Problem in Shooting Accuracy using Raycast
Here is The Script :
var hit: RaycastHit;
if (Physics.Raycast(SpawnPoint.position, SpawnPoint.forward, hit)){
var delay = hit.distance / bulletSpeed;
var hitPt = hit.point;
hitPt.y -= delay * 9.8;
var dir = hitPt - SpawnPoint.position;
yield WaitForSeconds(delay);
// then do the actual shooting:
if (Physics.Raycast(SpawnPoint.position, dir, hit)){
if(hit.collider.gameObject.name == "Cube1"){
hit.rigidbody.AddForceAtPosition(20 * SpawnPoint.forward , hit.point);
}
}
Debug.DrawRay(SpawnPoint.position,dir * 20,Color.red);
}
and the issue is
the Red Crosshair aiming on the box but when i shoot it draw ray but its next the box
the Accuracy is wrong
can anyone fix my script
accc.png
(95.8 kB)
Comment
That is probably, because your "spawnPoint" is not facing in the right direction.
Just as a note, all FPS games shoot from the camera, not the gun :)
thanks .. and ya the spawn point is exactly in the center of my camera
What do you mean by "the centre"? is it a child of the camera and only has an offset on the Z axis?