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.