- Home /
Spawning Particle Systems for Gun Sparks
I'm trying to make a particle system that shoots sparks in a way that is realistic, the sparks are rotated around the normal but I'm not sure how to do this. Help would be appreciated. The idea is that the object should track to the user but then rotate 180 degrees around the normal so that the right angle is achieved. Is this possible? I'm new-ish to using C# and am not 100% familiar with all of the Vector and Quaternion operations to use
Here's the script of the object that spawns the spark object.
public class BulletScript : MonoBehaviour {
public float bulletDist = 10000;
public GameObject sparkObj;
public GameObject decalObj;
public float distFromWall = 0.001f;
public RaycastHit hit;
public PistolScript weaponScript;
void Awake()
{
weaponScript = GameObject.FindGameObjectWithTag("Player").GetComponent<MoveControl>().currentWeapon.GetComponent<PistolScript>();
if (!Input.GetButton("Aim"))
{
transform.Translate(new Vector3(weaponScript.holdSide * -1, weaponScript.holdHeight * -1, 0));
}
if (Physics.Raycast(transform.position, transform.forward, out hit, bulletDist))
{
if ((sparkObj) && hit.transform.tag == "Shootable")
{
Instantiate(sparkObj, hit.point + (hit.normal * distFromWall), Quaternion.LookRotation(hit.normal));
Instantiate(decalObj, hit.point + (hit.normal * distFromWall), Quaternion.FromToRotation(Vector3.up, hit.normal), transform);
}
}
}
void Update ()
{
// Destroy(gameObject);
}
}
Answer by AshwinTheGammer · Sep 26, 2017 at 05:56 AM
try this:
public class BulletScript : MonoBehaviour {
public float bulletDist = 10000;
public GameObject sparkObj;
public GameObject decalObj;
public float distFromWall = 0.001f;
public RaycastHit hit;
public PistolScript weaponScript;
void Awake()
{
weaponScript = GameObject.FindGameObjectWithTag("Player").GetComponent<MoveControl>().currentWeapon.GetComponent<PistolScript>();
}
function Update () {
if (!Input.GetButton("Aim"))
{
transform.Translate(new Vector3(weaponScript.holdSide * -1, weaponScript.holdHeight * -1, 0));
}
if (Physics.Raycast(transform.position, transform.forward, out hit, bulletDist))
{
if ((sparkObj) && hit.transform.tag == "Shootable")
{
GameObject spark = Instantiate(sparkObj, hit.point + (hit.normal * distFromWall), Quaternion.LookRotation(hit.normal));
GameObject decal = Instantiate(decalObj, hit.point + (hit.normal * distFromWall), Quaternion.FromToRotation(Vector3.up, hit.normal), transform);
}
}
}
}
Your answer
Follow this Question
Related Questions
How to prevent particle system from restarting when value changes from script? 1 Answer
error CS0246: The type or namespace name `Particle' could not be found. 1 Answer
Customising Particle Effects for exploding rocks?? 1 Answer
[Math] Why is this possible ? 1 Answer
Rotation to align between two vectors but with preserved Camera Z axis 0 Answers