简体   繁体   English

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

[英]Unity Particle system: Change Emitter Velocity with Script

I have a particle system connected with an object that it follows.我有一个粒子系统,它与一个 object 相连。 The Emitter Velocity is here set on Rigidbody.发射器速度在这里设置在刚体上。 What I want is to have the particle system follow the object, as it does, but when detecting a touch input the particles are to follow the touch input, changing the Emitter Velocity to Transform.我想要的是让粒子系统跟随 object,就像它一样,但是当检测到触摸输入时,粒子将跟随触摸输入,将发射器速度更改为变换。 When running the code that I attached, there are two compiler errors that I have tried and failed to fix.运行我附加的代码时,有两个编译器错误我尝试过但未能修复。 Would appreciate someone taking a look at it.将不胜感激有人看看它。

  • The 'Particle System' does not contain a definition for 'emitterVelocity' and no accessible extension method 'emitterVelocity' accepting a first argument of type 'ParticleSystem' could be found. “粒子系统”不包含“emitterVelocity”的定义,并且找不到接受“ParticleSystem”类型的第一个参数的可访问扩展方法“emitterVelocity”。 line 28.第 28 行。
  • 'Transform' is a type, which is not valid in the given context. 'Transform' 是一种类型,在给定的上下文中无效。 line 28.第 28 行。
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;
        }
    }
}

First, when trying to access the Transform to which a Unity component is attached, you want to use transform (note lowercase "t" vs. uppercase).首先,当尝试访问附加了 Unity 组件的Transform时,您需要使用transform (注意小写“t”与大写)。 Switch Transform to transform or this.transform . Transform切换到transformthis.transform

transform is a property that all MonoBehaviours have that gives the same value as calling this.GetComponent<Transform>() . transform是所有MonoBehaviours都具有的属性,它提供与调用this.GetComponent<Transform>()相同的值。 By contrast, Transform is the type UnityEngine.Transform , which is to say there exists a class with that name.相比之下, TransformUnityEngine.Transform类型,也就是说存在一个具有该名称的 class。

Second, in regards to setting the emitter, you can set the emitterVelocityMode (labelled as "Emitter Velocity") in the particle system's main component .其次,关于设置发射器,可以在粒子系统的main组件中设置emitterVelocityMode (标记为“Emitter Velocity”)。 The values for emitterVelocityMode are an enum named "ParticleSystemEmitterVelocityMode" . emitterVelocityMode的值是一个名为 "ParticleSystemEmitterVelocityMode" 的枚举

You can say:你可以说:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM