Question by
Namnam · Jan 22, 2017 at 12:26 AM ·
raycasthitlinecastthirdpersoncontrollerthird person shooter
Third person shooter need some help
Hi,
I have a third person model and script to move and aiming. Now i want to make it shoot and instantiate bullet hole at hit point.
What i have done : Make a raycast from center of camera to camera.forward direction. Then I made a linecast from gun to the hit point of the camera raycast. I have draw the rays with Debug.DrawLine and the two ray go exactly at the same position namely the camera hit point.
The problem is that the ray of the gun never hit something so it doesn't instantiate gun holes and I don't see "hit gun" in the console.
What is wrong please ?
Need help because i don't see any tutorial and I don't know what is wrong.
Here is my code:
public void shootOne() {
if (Time.time > lastShoot + firerate) {
Vector3 fwd = Camera.main.transform.forward;
fwd.y += 0.32f;
Vector3 centerCamera = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2,Screen.height/2,0));
RaycastHit hitCam;
RaycastHit hit;
if (Physics.Raycast (centerCamera, fwd, out hitCam, 100)) {
print ("cam hit");
Vector3 direction = hitCam.point;
if(Physics.Linecast(instantiatePoint.position,direction,out hit,500)) {
print ("gun hit");
GameObject bulletHoleObject = (GameObject)Instantiate (bulletHole, hit.point, Quaternion.LookRotation (hit.normal));
bulletHoleObject.transform.up = hit.normal;
}
Debug.DrawLine (Camera.main.transform.position, direction, Color.green);
Debug.DrawLine (instantiatePoint.position, direction, Color.red);
}
lastShoot = Time.time;
}
}
Comment