首页 > 解决方案 > Unity 粒子系统:使用脚本更改发射器速度

问题描述

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

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;
        }
    }
}

标签: c#unity3dcompiler-errorsparticle-system

解决方案


首先,当尝试访问TransformUnity 组件所附加到的 时,您想要使用transform(注意小写“t”与大写)。切换Transformtransformthis.transform

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

其次,关于设置发射器,您可以在粒子系统的组件emitterVelocityMode中设置(标记为“发射器速度”)。的值是一个名为 "ParticleSystemEmitterVelocityMode" 的枚举mainemitterVelocityMode

你可以说:

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

推荐阅读