在制作游戲過程中會出現鼠標滑動的實現,而為啦增強視覺效果,往往會添加拖尾或者鼠標點擊的特效,接下來就說說在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里面用來實時定位鼠標按下去的坐標