- Home /
aiming error
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
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<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
}
}
}
shoot.png
(284.2 kB)
Comment
It also seems to eventually work after spam$$anonymous$$g left click on the object
Your answer
![](https://koobas.hobune.stream/wayback/20220612222500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why are my guns shooting lower when my character moves forward? 1 Answer
Using raycast gun to take down life of enemy when shot 1 Answer
not losing ammo when shooting 1 Answer
Raycast shooting 1 Answer
Trouble with Raycast shooting 1 Answer