My raycast's origin is randomly moving up and down randomly so my gun script is inconsistent. Can someone help me with this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fire : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public GameObject impactEffect;
public ParticleSystem muzzleflash;
public Camera fpsCam;
//Sound
public AudioSource Pew;
public void PlayPew()
{
Pew.Play ();
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
Pew.Play ();
Shoot();
}
}
void Shoot ()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
Target target = hit.transform.GetComponent();
if (target != null)
{
target.TakeDamage(damage);
}
GameObject impact = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impact, 0.25f);
}
}
}
↧