- Home /
Different bullet effect on different physics materals.
Hello, i'm trying create a weapon system, i have succeeded to make it shoot and damage objects.
However i have not succeeded to make different particle effects for different materials
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gun : MonoBehaviour
{
public float damage = 60f;
public float kick = 5f;
public float range = 150f;
public float firerate = 15f;
public Camera FPSCAM;
public ParticleSystem muzzleflash;
public GameObject stoneparticlehit;
public GameObject woodparticlehit;
public GameObject sandparticlehit;
private float nextfire = 0f;
public PhysicMaterial Stone;
public PhysicMaterial Wood;
public PhysicMaterial Sand;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextfire)
{
nextfire = Time.time + 1f / firerate;
shoot();
}
}
public void shoot()
{
RaycastHit Hit;
muzzleflash.Play();
if (Physics.Raycast(FPSCAM.transform.position, FPSCAM.transform.forward, out Hit, range))
{
Target target = Hit.transform.GetComponent<Target>();
if(target != null)
{
target.takedamage(damage);
}
if (Hit.rigidbody != null)
{
Hit.rigidbody.AddForce(-Hit.normal * kick);
}
Collider hit = Hit.transform.GetComponent<Collider>();
if (hit.material == Stone)
{
Instantiate(stoneparticlehit, Hit.point, Quaternion.LookRotation(Hit.normal));
}
if (hit.material == Wood)
{
Instantiate(woodparticlehit, Hit.point, Quaternion.LookRotation(Hit.normal));
}
if (hit.material == Sand)
{
Instantiate(sandparticlehit, Hit.point, Quaternion.LookRotation(Hit.normal));
}
}
}
}
Any ideas?
Do you get an error or simply nothing happens? Why don't you start with debugging hit.material
?
Nothing happens when i shoot (no impact effects, otherwise works fine).
I'm going to try the debugging.
Answer by Shooterman · Oct 19, 2020 at 06:13 PM
Try using a tag link provided link text
An example would be something like this.
if (hit.transform.tag == "Sand")
However you have to make sure your object is tagged and the spelling of your tag is exact. If you put this right before your Instantiate then you can remove all your public game objects for different materials in place of tags. I strongly recommend using Debug.Log to insure your hitting what you want also.