My shooting script puts in the console what has been shot at, it works for 2 clicks or so and then doesnt work after that. It uses ray casts and seems to target what isnt centre of the screen( the crosshairs)
Here is my screenshot and script
![alt text][1]
using UnityEngine;
public class Shooting : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent();
if (target != null)
{
target.TakeDamage(damage);
}
}
}
}
[1]: /storage/temp/152099-shoot.png
↧