- Home /
Particle System Instantiate's With Original Rotation - C#
I created a particle system that emits sparks when a Raycast hits a collider. It works, and instantiates properly, except for the rotation of the particle system always keeps its original setting. It always emits the same direction. I have a feeling it has to either do with my Raycast not returning a proper rotation from the hitInfo (or returning one at all), or from the Quaternion.identity not being what I need to translate the rotation properly. Any help on the issue would be great; I'm not looking for someone to write the code for me, I'm just looking to better understand how to accomplish getting particle system's to instantiate properly. The particle effect is added as hitPrefab, and I have tried wrapping it in another empty Game Object.
Here's my code so far:
using UnityEngine;
using System.Collections;
public class PerformsAttack : MonoBehaviour
{
public float range = 100f;
public float cooldown = 0.2f;
float cooldownRemaining = 0;
public float damage = 25f;
public GameObject hitPrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
cooldownRemaining -= Time.deltaTime;
if(Input.GetMouseButton(0) && cooldownRemaining <= 0)
{
cooldownRemaining = cooldown;
Ray ray = new Ray( Camera.main.transform.position, Camera.main.transform.forward );
RaycastHit hitInfo;
if( Physics.Raycast(ray, out hitInfo, range))
{
Vector3 hitPoint = hitInfo.point;
GameObject go = hitInfo.collider.gameObject;
Debug.Log ("Hit Object: " + go.name);
Debug.Log ("Hit Point: " + hitPoint);
HasHealth h = go.GetComponent<HasHealth>();
if(h != null)
{
h.RecieveDamage(damage);
}
if(hitPrefab != null)
{
Instantiate( hitPrefab, hitPoint, Quaternion.identity);
}
}
}
}
}
Answer by Benoit Dufresne · Jan 20, 2014 at 12:31 AM
Instantiate( hitPrefab, hitPoint, Quaternion.identity);
This spawns your particle system with a Quaternion.identity rotation. Try using the rotation you want instead!
Try
Quaternion.LookRotation(ray.normal)
"ray.normal" was giving me error: "UnityEngine.Ray does not contain a definition for normal..."
However, I had already gathered the normal info from the Raycast separately (without knowing it) and placed it into "hitInfo".
I simply swapped "ray.normal" over to "hitInfo.normal" and it started working like a charm.
Thanks a lot Benoit Dufresne; I had been stuck on this for about 5 hours now! You certainly led me to water on this one.
Oops yes, hitInfo was what I meant!
And no problem, just starting to give back some of all the help I got around here, now that I actually know lots of those twists and turns.