所谓互动草,就是角色跑动或者释放技能,能影响草的摆动方向和幅度.
前面的文章早已经实现了风吹草动的效果,迟迟没有在Unity上面做互动草,是因为以前我在端游项目做过一套太过于牛逼的方案.在CE3的互动草的基础上扩展,效果好,但技术太复杂,效率开销也特别高. 如果在手机上,就得做一套简单高效的.
实现效果:从任意方向碰一下草,草就应该来回晃动,晃动幅度逐渐减小.多次触碰,效果应该叠加.这样的话就比较真实.
实现原理:用正玄波实现草来回摆动的简谐运动,用指数衰减来模拟阻力
实现步骤:
1.每个草挂一个脚本,来处理力的效果叠加
public class Force
{
public float m_Time = 0;
public Vector4 m_Force;
public Force(Vector4 force)
{
m_Force = force;
}
}
public class GrassForce : MonoBehaviour
{
public List<Force> m_ForceList = null;
public float m_WaveFrequency = 6.0f;
public float m_Resistance = 0.25f;
public float m_MaxForceMagnitude = 6.0f;
public float m_AddForceTimeInterval = 0.5f;
public int m_MaxForceNum = 3;
private float m_LastAddTime = 0;
private Material material;
void Start()
{
material = gameObject.renderer.material;
}
void Update()
{
UpdateForce();
}
void OnBecameVisible()
{
enabled = true;
}
void OnBecameInvisible()
{
enabled = false;
}
public void AddForce(Vector3 force)
{
if (Time.time - m_LastAddTime > m_AddForceTimeInterval)
{
m_LastAddTime = Time.time;
if (m_ForceList == null)
m_ForceList = new List<Force>();
if (m_ForceList.Count < m_MaxForceNum)
{
Vector4 newForce = new Vector4(force.x, 0, force.z, 0);
if (newForce.magnitude > m_MaxForceMagnitude)
newForce = newForce.normalized * m_MaxForceMagnitude;
m_ForceList.Add(new Force(newForce));