public class Weapon : MonoBehaviour
{
[SerializeField] private float range = 100f;
[SerializeField] private float damage = 40f;
[SerializeField] private float impactForce = 30f;
[SerializeField] private float fireRate = 25f;
[SerializeField] private ParticleSystem nozzleEffect;
[SerializeField] private ParticleSystem impactEffect;
private Camera mainCamera;
private float nextShootTime;
private float nextTimeToFire = 0f;
// Start is called before the first frame update
void Start()
{
nextShootTime = Time.deltaTime;
mainCamera = Camera.main;
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1 / fireRate;
Shoot();
}
}
void Shoot()
{
nozzleEffect.Play();
Ray ray = new Ray(mainCamera.transform.position, mainCamera.transform.forward);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, range))
{
Target target = hitInfo.collider.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hitInfo.rigidbody != null)
{
hitInfo.rigidbody.AddForce(-hitInfo.normal * impactForce, ForceMode.Impulse);
}
ParticleSystem ImpactEffect = Instantiate(impactEffect, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
Destroy(ImpactEffect.gameObject, 2f);
}
}
}
I have used unity 1st person starter assets and combined a weapon with it to make it a fps. Now I want a particle effect “impact effect” to be spawned at the place where my ray hits something
But if I press w for forward with any key ,particle is getting spawned at player’s head.
How to fix this and spawn particles at the place the ray hits.