Unity 粒子系统:使用脚本更改发射器速度

Unity Particle system: Change Emitter Velocity with Script

我有一个粒子系统与其跟随的对象相连。发射器速度在这里设置在刚体上。我想要的是让粒子系统像它一样跟随物体,但是当检测到触摸输入时,粒子将跟随触摸输入,将 Emitter Velocity 更改为 Transform。当 运行 我附加的代码时,有两个编译器错误,我已尝试修复但未能修复。如果有人看一下,我将不胜感激。

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

public class DragFingerMove : MonoBehaviour
{
    private Vector3 touchPosition;
    private ParticleSystem ps;
    private Vector3 direction;
    private float moveSpeed = 10f;

    // Use this for initialization
    private void Start()
    {
        ps = GetComponent<ParticleSystem>();
    }

    // Update is called once per frame
    private void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
            touchPosition.z = 0;
            direction = (touchPosition - transform.position);
            ps.emitterVelocity = Transform;
            ps.velocity = new Vector2(direction.x, direction.y) * moveSpeed;

            if (touch.phase == TouchPhase.Ended)
                ps.velocity = Vector2.zero;
        }
    }
}

首先,在尝试访问附加了 Unity 组件的 Transform 时,您需要使用 transform(注意小写 "t" 与大写)。将 Transform 切换为 transformthis.transform.

transform 是所有 MonoBehaviours 具有的 属性,它给出与调用 this.GetComponent<Transform>() 相同的值。相比之下,Transform 是类型 UnityEngine.Transform,也就是说存在具有该名称的 class。

其次,关于发射器的设置,可以在particle system's main component. The values for emitterVelocityMode are an enum named "ParticleSystemEmitterVelocityMode".

中设置emitterVelocityMode(标记为"Emitter Velocity")

你可以说:

var ps_main = GetComponent<ParticleSystem>().main;
ps_main.emitterVelocityMode = ParticleSystemEmitterVelocityMode.Transform;