- Home /
Question by
wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jul 16, 2018 at 03:33 PM ·
raycast3dfpsshooting
FPS shooting
Hi! I'm currently making a 3D FPS game.
public PlayerWeapon weapon;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
void Start()
{
if (cam == null)
{
Debug.LogError("PlayerWeapon: No camera referenced!");
this.enabled = false;
}
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, weapon.range, mask))
{
// We hit something
Debug.Log("We hit " + hit.collider.name);
}
}
&
[System.Serializable]
public class PlayerWeapon
{
public string name = "Glock";
public float damage = 10f;
public float range = 100f;
}
These are my codes related to shooting. However, When I run the program, and click fire, nothing comes out on my console. How should I fix it???
Comment
Answer by bolkay · Jul 16, 2018 at 05:36 PM
Make sure you have assigned all variables in the inspector. E.g. LayerMask. Also, it appears that you have not obtained reference to the camera object. Consider adding this to your Start method
cam=GetComponent<Camera>();
Your answer
Follow this Question
Related Questions
Advanced FPS shooting 1 Answer
How do i make a Railgun style effect with a raycast shot? 1 Answer
How can I make a Grapple Gun work with the Character Controller? 1 Answer
raycast spawn problem 0 Answers
shooting multiple enemies using raycast 2 Answers