Quantcast
Channel: Questions in topic: "gun"
Viewing all articles
Browse latest Browse all 592

Getting a crosshair to project onto the object a barrel is aiming at

$
0
0
Hello, I am trying to get a crosshair to project onto the object that the player is aiming at (like the Resident Evil 4's aiming system is). This means that crosshair should appear smaller as objects further away at looked at, and should appear larger as closer objects are looked at. This is for a 3rd person shooter setup. I currently have a crosshair gameobject as a child to a gun model. When the y position is zero, the crosshair is at the barrel of the gun. Any positive value of y will have the crosshair in the direction of the gun. Below is the code I've written so far. using UnityEngine; using System.Collections; public class gunControl : MonoBehaviour { public GameObject crosshair; public Vector3 bulletSpawnPoint; void Update () { RaycastHit hit; Vector3 dir = Camera.main.transform.forward; if (Physics.Raycast (bulletSpawnPoint, dir, out hit)) { crosshair.transform.position = hit.point; crosshair.transform.LookAt (Camera.main.transform); } } } The crosshair gameobject is my crosshair and the bulletSpawnPoint is the vector3 of the spawn point in which bullets are created. My theory was to use a RaycastHit to find the object being aimed at, and then to use a Physics.Raycast as a conditional. But, sadly, this does not work as it sits, the crosshair does nothing but sit at its default position relative to the gun barrel.

Viewing all articles
Browse latest Browse all 592

Trending Articles