- Home /
why is my raycast not picking up on the enemy?
here is my code. for some reason, the raycast is only working when I go very near the enemy, like touching it, and that's obviously not what I want. so check out my code to see whats wrong.
private enemyMovement em;
private Rigidbody rb;
public GameObject muzzleFlash;
public float recoil;
public Transform firepoint;
public GameObject bullet;
public int damage = 100;
public GameObject tr;
public TrailRenderer trail;
bool trsmall;
public AudioSource shootingSound;
private float timer = 0;
public float reloadTime = 1;
public Transform fire;
public Transform fire1;
public Transform fire2;
public Transform fire3;
public Transform fire4;
public Transform fire5;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (Input.GetMouseButtonDown(0))
{
if (timer >= reloadTime)
{
shootingSound.Play();
timer = 0;
GameObject trailEff = (GameObject)Instantiate(tr, firepoint.position, Quaternion.identity);
trail = trailEff.GetComponent<TrailRenderer>();
trail.startWidth = 0.15f;
Destroy(trailEff, 3f);
GameObject effect = (GameObject)Instantiate(muzzleFlash, firepoint.position, Quaternion.identity);
Destroy(effect, 2f);
RaycastHit hit;
bool canShoot = Physics.Raycast(fire.position, fire.forward, out hit, 1000000f);
if (canShoot)
{
if (hit.transform.CompareTag("enemy"))
{
Debug.Log(hit.transform.gameObject);
em = hit.transform.GetComponentInParent<enemyMovement>();
em.health -= damage;
}
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Raycast damage script 2 Answers
Raycast Shooting problem 2 Answers
Raycast Shooting Help 1 Answer
raycast is pointing in two directions? 1 Answer
Trouble with Raycast shooting 1 Answer