c# - Unity粒子系统: Change Emitter Velocity with Script

标签 c# unity3d compiler-errors particle-system

我有一个粒子系统与其后跟的对象相连。发射器速度在此处设置为刚体。我想要的是像这样使粒子系统跟随对象,但是当检测到触摸输入时,粒子将跟随触摸输入,将“发射器速度”更改为“变换”。在运行我附加的代码时,我尝试了两个且未能修复的编译器错误。希望有人来看看。

  • “粒子系统”不包含
    'emitterVelocity',并且没有可访问的扩展方法
    'emitterVelocity'接受类型为'ParticleSystem'的第一个参数
    可以找到。第28行。
  • 'Transform'是一种类型,在给定的上下文中无效。
    第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;
            }
        }
    }
    

    最佳答案

    首先,当尝试访问附加了Unity组件的Transform时,您要使用transform(注意小写的“t”和大写的字母)。将Transform切换为transformthis.transform
    transform是所有MonoBehaviours都具有的属性,其属性与调用this.GetComponent<Transform>()相同。相比之下,TransformUnityEngine.Transform类型,也就是说存在一个具有该名称的类。

    其次,关于设置发射器,您可以在particle system's emitterVelocityMode component中设置main(标记为“Emitter Velocity”)。 emitterVelocityMode的值为an enum named "ParticleSystemEmitterVelocityMode"

    你可以说:

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

    关于c# - Unity粒子系统: Change Emitter Velocity with Script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62066754/

    相关文章:

    C# 泛型和集合

    c# - 如何将 List<string> 绑定(bind)到 ItemsControl?

    c# - 如何将服务注入(inject)AutoMapper?

    C# - 编译器错误 - 将 int[] 分配给 object[]

    c# - Entity Framework 是否需要预取路径?

    c# - 更快的 SQL 插入?

    android - Proguard 与 Unity3d 返回错误代码 1

    c# - 使用2个单独的按钮Unity播放和停止音频

    node.js - 在终端中使用 tsc 编译类型脚本时出现类型脚本错误

    java - 添加到通用列表<?> 产生编译错误