Оля
Оля

Reputation: 81

How to attach the particle system to the collider Or how to move the particle system with the character

Does somebody have any idea how to attach particle system to the collider in the script? I have my character and I want to have blood particle system on the position of the tap on the head. I have managed to do this with the code below but now I need to move it together with the collider(with the character). Because when I move my character(I use LeanTouch script for this) the blood is left where it was created on the scene. The code I use, it is on the Camera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActionOnTapOrClick : MonoBehaviour {

    public ParticleSystem blood;

    private void Update()
    {

        if(Input.GetMouseButtonDown(0))
        {
            Ray toTouch = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit rhInfo;
            bool didHit = Physics.Raycast(toTouch, out rhInfo);
            if(didHit && rhInfo.collider != null )
            {
                Debug.Log("You've tapped on the " + rhInfo.collider.name);
                blood.transform.position = rhInfo.point;
                Instantiate(blood, rhInfo.point, transform.rotation);
            }
            else { Debug.Log("You need to tap on the head!"); }

        }
    }
}

Upvotes: 0

Views: 98

Answers (3)

Karolis Jc
Karolis Jc

Reputation: 106

You should put in as a child object. This should work.

Instantiate(blood, rhInfo.point, transform.rotation, rhInfo.point.transform);

Upvotes: 1

vmchar
vmchar

Reputation: 1323

You're doing it right. All you need to do is to add your blood object as a child, so you can do something like this : var ps = Instantiate(blood, rhInfo.point, transform.rotation); ps.transform.parent = transform; So check out this and this depending on your Unity version

Upvotes: 2

Marco Salerno
Marco Salerno

Reputation: 5201

You must attach the particle to the character object

Upvotes: 0

Related Questions