gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:09:54 26 4
gpt4 key购买 nike

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

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

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com