- Home /
Question by
YoloMaster919 · May 13, 2020 at 03:30 AM ·
raycastgungun scriptoriginrandomly
Raycast origin random
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<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
GameObject impact = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impact, 0.25f);
}
}
}
Comment
How exactly are you using the camera? are you using Cinamachine? It might also be that the gun or camera is moving when you shoot, so it might be the reason, but there is very little information to work with here.