- Home /
How to shoot a raycast with third person
Hi, I have a third person model with controls for move and a camera look at him and follow. Now i'm looking for make him shoot with a RayCast but I get some problem. The raycast doesn't hit at the crosshair but above or beside and can't adjust it with Vector3 because it change according to the distance.
I have tried to cast a raycast from camera to camera forward and adjust the y of hit.point then i make a raycast from gun to the direction of hit.point but doesnt' work.
I tried the same with a raycast from camera to the camera crosshair.
How make simple third person shooter please because i don't see any tutorial and i wondering what wrong and if it's the good way to do it.
code here:
RaycastHit cameraHit;
Ray cameraAim = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2));
Physics.Raycast (cameraAim, out cameraHit, 100f);
Debug.DrawLine (cameraAim.origin, cameraHit.point, Color.green);
Vector3 cameraHitPoint = cameraHit.point;
float cameraDistance = cameraHit.distance;
if (Physics.Raycast (cameraAim, out cameraHit)) {
// something was hit
RaycastHit playerHit;
Physics.Raycast (instantiatePoint.position + new Vector3 (0f, 0f, 0f), cameraHitPoint - instantiatePoint.position - new Vector3 (0f, 0f, 0f), out playerHit, 100f);
Debug.DrawLine (instantiatePoint.position + new Vector3 (0f, 1.8f, 0f), cameraHitPoint - transform.position - new Vector3 (5.0f, 10, 0f), Color.red);
if (Physics.Raycast (instantiatePoint.position + new Vector3 (0f, 0f, 0f), (cameraHitPoint - transform.position) + new Vector3 (0f, 0.8f, 0f), out playerHit, 100f)) {
GameObject bulletHoleObject = (GameObject)Instantiate (bulletHole, playerHit.point, Quaternion.LookRotation (playerHit.normal));
bulletHoleObject.transform.up = playerHit.normal;
}
float playerDistance = playerHit.distance;
Debug.Log ("Distance from player: " + playerDistance);
Debug.Log ("Distance from camera: " + cameraDistance);
}
Thanks in advance.
Answer by AurimasBlazulionis · Jan 18, 2017 at 02:21 PM
Shoot a ray from the camera forward. Then, once you have the position, do a Linecast from the weapon to the camera's ray hit point.
You might want to check if the hit is not behind the player. To check that, do: characterTransform.InverseTransformPoint(hit.point).z < 0.
I done what you have advised me and i have adjusted the y of the point of the raycast of the camera in order to match with the crosshair because the camera is above the player and she look at him but the impact of the bullet is above or beside the crosshair according to the distance with the player and camera, so i can't adjuste the point of the raycast of the camera to match with the crosshair. Furthermore, sometime it doesn't make bullet's impact. What is wrong with my code ?
here is my code :
public void shootOne() {
if (Time.time > lastShoot + firerate) {
Vector3 fwd = Camera.main.transform.TransformDirection(Vector3.forward);
Vector3 direction = new Vector3 ();
RaycastHit hitCam;
if (Physics.Raycast (Camera.main.transform.position, fwd, out hitCam, 500)) {
direction = hitCam.point;
}
Debug.DrawLine (Camera.main.transform.position, direction, Color.green);
RaycastHit hit;
Debug.DrawLine (instantiatePoint.position, direction, Color.red);
direction.y += 18.5f;
if(Physics.Linecast(instantiatePoint.position,direction,out hit)) {
GameObject bulletHoleObject = (GameObject)Instantiate (bulletHole, hit.point, Quaternion.LookRotation (hit.normal));
bulletHoleObject.transform.up = hit.normal;
}
lastShoot = Time.time;
}
}
Your answer
Follow this Question
Related Questions
Touch different Gameobjects using camera array and raycast hit 3 Answers
Problem With Raycasts, Interaction Script. 1 Answer
Need help with Third person shooter 0 Answers
Is object at least partly visible? 1 Answer
Can someone modify this for me? 0 Answers