c# - Unity 粒子系统:使用脚本更改发射器速度
问题描述
我有一个粒子系统,它与它所遵循的对象相连。发射器速度在这里设置在刚体上。我想要的是让粒子系统像它一样跟随对象,但是当检测到触摸输入时,粒子将跟随触摸输入,将发射器速度更改为变换。运行我附加的代码时,有两个编译器错误我尝试过但未能修复。将不胜感激有人看看它。
- “粒子系统”不包含“emitterVelocity”的定义,并且找不到接受“ParticleSystem”类型的第一个参数的可访问扩展方法“emitterVelocity”。第 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;
}
}
}
解决方案
首先,当尝试访问Transform
Unity 组件所附加到的 时,您想要使用transform
(注意小写“t”与大写)。切换Transform
到transform
或this.transform
。
transform
是一个属性,所有属性都MonoBehaviours
具有与调用相同的值this.GetComponent<Transform>()
。相比之下,Transform
是 type UnityEngine.Transform
,也就是说存在一个具有该名称的类。
其次,关于设置发射器,您可以在粒子系统的组件emitterVelocityMode
中设置(标记为“发射器速度”)。的值是一个名为 "ParticleSystemEmitterVelocityMode" 的枚举。main
emitterVelocityMode
你可以说:
var ps_main = GetComponent<ParticleSystem>().main;
ps_main.emitterVelocityMode = ParticleSystemEmitterVelocityMode.Transform;
推荐阅读
- typescript - 从其他包扩展接口
- azure - ARM 模板 Key Vault 访问策略以分组而不是身份原则
- vb.net - 自定义按钮控件中的圆角柱
- css - CSS 文本空白扩展为填充
- rest - 如何集成 Spring MVC 和 Nuxt JS?
- powershell - 使用 powershell 将数据复制到不同的驱动器
- node.js - 如何使用 webpack 节点 API 执行编译后的代码?
- c# - 使用外部 dll 函数时 C++ 访问冲突
- java - Spring Boot 应用无法启动 | 无法将“spring.data.mongodb.host”下的属性绑定到 java.lang.String
- apache-flink - Flink 在运行时是否知道 Kafka 分区添加