在制作游戏过程中会出现鼠标滑动的实现,而为啦增强视觉效果,往往会添加拖尾或者鼠标点击的特效,接下来就说说在unity中如何实现拖尾效果,首先unity中有个组件,叫TrailRenderer,在Component ——> Effect——>TrailRenderer中添加,根据自己需要的效果设置自己需要的颜色,然后还有相应的曲线和拖尾长度,来实现自己需要的效果,接下来上代码
public TrailRenderer m_CollectTrail = null; #if UNITY_EDITOR
if (Input.GetMouseButtonDown(0))
{
if (!m_PointDown)
{
m_PointDown = true;
m_CollectTrail.Clear();
m_CollectTrail.gameObject.SetActive(true);
}
}
else if (Input.GetMouseButtonUp(0))
{
if (m_PointDown)
{
m_PointDown = false;
m_CollectTrail.gameObject.SetActive(false);
}
}
#else
if (Input.touchCount > 0)
{
if (!m_CollectTrail.gameObject.activeSelf)
{
m_CollectTrail.Clear();
m_CollectTrail.gameObject.SetActive(true);
}
}
else
{
if (m_CollectTrail.gameObject.activeSelf)
{
m_CollectTrail.gameObject.SetActive(false);
}
}
#endif
// Update trail position.
if (m_CollectTrail.gameObject.activeSelf)
{
var screenPos = (Application.isMobilePlatform && (Input.touchCount > 0)) ? (Vector3)(Input.GetTouch(0).position) : Input.mousePosition;
var worldPos = Camera.main.ScreenToWorldPoint(screenPos);
worldPos.z = 0.0f;
m_CollectTrail.transform.position = worldPos;
}
}
放在Undate里面用来实时定位鼠标按下去的坐标