【问题标题】:Unity Particle system: Change Emitter Velocity with ScriptUnity 粒子系统:使用脚本更改发射器速度
【发布时间】:2020-05-28 23:19:53
【问题描述】:

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

  • “粒子系统”不包含对 '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;
        }
    }
}

【问题讨论】:

    标签: c# unity3d compiler-errors particle-system


    【解决方案1】:

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

    transform 是所有MonoBehaviours 都具有的属性,它提供与调用this.GetComponent&lt;Transform&gt;() 相同的值。相比之下,TransformUnityEngine.Transform 类型,也就是说存在一个具有该名称的类。

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

    你可以说:

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

    【讨论】:

    • 感谢您的回答!这是检查员的屏幕截图:imgur.com/a/KLgSK5F
    • 答案已相应更新。希望对您有所帮助。
    猜你喜欢
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多