首先,产生特效无非就是生成一个特效物体。但是怎么确定生成的位置呢?
我希望是和鼠标的位置一样,但是鼠标点击Input.MousePosition的坐标系和GameObject所在是世界坐标系怎么转换呢?
using UnityEngine;
public class ClickEffect : MonoBehaviour {
Vector3 point;
GameObject effectGo;
void Start () {
effectGo = Resources.Load<GameObject>("Prefabs/EffectClick");
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
point = new Vector3(Input.mousePosition.x,Input.mousePosition.y,4f);//获得鼠标点击点
point = Camera.main.ScreenToWorldPoint(point);//从屏幕空间转换到世界空间
GameObject go = Instantiate(effectGo);//生成特效
go.transform.position = point;
Destroy(go, 0.5f);
}
}
}
本文介绍了一个Unity中的简单脚本,该脚本可以在鼠标点击时生成特效。通过将鼠标位置从屏幕坐标转换为世界坐标,实现了特效物体在点击位置的准确生成,并设置了0.5秒后销毁特效。
961

被折叠的 条评论
为什么被折叠?



