I have a particle effect that looks like sparks. It plays whenever a player hits a certain object with a raycast. The problem is that the particle effect is placed wherever the player hits the object, often in the middle of the mesh so it looks strange. Would there be a way to make the sparks always play on a certain part of the object (like on the top of it?) Or (even better) make the particle effect’s shape the same shape as the object so it looks like the entire object is making sparks?
Thanks!
Here is my incredibly confusing script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DecayRay : MonoBehaviour
{
private int count = 1;
public GameObject sparks; //The particle effect
private GameObject obj;
private int cd = 1;
RaycastHit hit;
IEnumerator transform2()
{
sparks.transform.position = hit.point; //this changes the particle effect position to wherever the player hits the object.
sparks.SetActive(true);
yield return new WaitForSeconds(2);
sparks.SetActive(false);
obj.GetComponent<DecayInto>().startObject.SetActive(true);
obj.GetComponent<DecayInto>().decayObject.SetActive(false);
cd = 1;
}
IEnumerator transform1()
{
sparks.transform.position = hit.point;
sparks.SetActive(true);
yield return new WaitForSeconds(2);
sparks.SetActive(false);
obj.GetComponent<DecayInto>().startObject.SetActive(false);
obj.GetComponent<DecayInto>().decayObject.SetActive(true);
cd = 1;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (((Physics.Raycast(transform.position, transform.forward, out hit) && hit.transform.tag == "cube") && count == 1) && cd == 1)
{
cd = 0;
obj = hit.transform.gameObject;
StartCoroutine(transform1());
count = 2;
}
else if (((Physics.Raycast(transform.position, transform.forward, out hit) && hit.transform.tag == "cube") && count == 2) && cd == 1)
{
cd = 0;
StartCoroutine(transform2());
count = 1;
}
else
{
//nothin.
}
}
}
}