所谓互动草,就是角色跑动或者释放技能,能影响草的摆动方向和幅度.
前面的文章早已经实现了风吹草动的效果,迟迟没有在Unity上面做互动草,是因为以前我在端游项目做过一套太过于牛逼的方案.在CE3的互动草的基础上扩展,效果好,但技术太复杂,效率开销也特别高. 如果在手机上,就得做一套简单高效的.
实现效果:从任意方向碰一下草,草就应该来回晃动,晃动幅度逐渐减小.多次触碰,效果应该叠加.这样的话就比较真实.
实现原理:用正玄波实现草来回摆动的简谐运动,用指数衰减来模拟阻力
实现步骤:
1.每个草挂一个脚本,来处理力的效果叠加
2.如何确定哪些草受到影响,以及受力的方向?
- public static void AddForceToGrass(int forceId, Transform transform)
- {
- ForceTable force = ForceTableMgr.Instance.GetDataById(forceId);
- if (force != null)
- {
- Vector3 relativeCenter = new Vector3(force.RelativeCenterX, force.RelativeCenterY, force.RelativeCenterZ);
- Vector3 center = transform.TransformPoint(relativeCenter);
-
- Vector3 size = new Vector3(force.Width, force.Height, force.Length);
-
-
- Matrix4x4 m44 = Matrix4x4.TRS(Vector3.zero, Quaternion.Inverse(transform.rotation), Vector3.one);
-
- PhysicsUtil.AddForceToGrass((RangeType)force.RangeType, (ForceDirType)force.DirType, force.Strength, center, size, transform.forward, m44, force.Degree);
- }
- }
-
- private static void AddForceToGrass(RangeType type, ForceDirType dirType, float strength, Vector3 center, Vector3 size, Vector3 direction, Matrix4x4 m44, float degree = 360.0f)
- {
- if (type == RangeType.Sphere)
- {
- AddForceInSector(dirType, strength, center, size.x, direction, degree);
- }
- else if (type == RangeType.Cude)
- {
- AddForceInCube(dirType, strength, center, size, direction, m44, degree);
- }
- }
草可以看成一个点,计算和下面范围的相交.
1.圆形和扇形范围
圆形范围计算特别简单,计算距离即可.扇形范围只需要在圆形基础上再计算一次夹角即可,部分核心代码:
- Vector3 dir = script.transform.position - center;
- if (dir.sqrMagnitude <= radius * radius)
- {
- dir.y = 0;
- if (Mathf.Abs(Vector3.Angle(dir, direction)) <= degree)
- {
- float factor = 0.25f + Mathf.Clamp01((radius - dir.magnitude) / radius) * 0.75f;
-
- Vector3 forceDir;
- if (dirType == ForceDirType.ToTarget)
- forceDir = dir.normalized;
- else
- forceDir = -dir.normalized;
-
- script.AddForce(forceDir * factor * strength);
- }
- }
2.矩形范围
点和任意方向的矩形的计算,这个比较难.Unity本身也没提供此类相交API.不过熟悉引擎开发的应该知道AABB和OBB吧.其实矩形范围计算,就是计算点和OBB的相交.
点和AABB的相交计算很简单,因为AABB每条边都是和坐标轴平行或者垂直的.而OBB有方向,其实只需要把点矩阵变换到OBB所在的空间,就可以用AABB的方法来计算了.
- public struct AABB
- {
- public Vector3 min;
- public Vector3 max;
-
- public AABB(Vector3 vmin, Vector3 vmax)
- {
- min = vmin;
- max = vmax;
- }
- }
-
- public struct OBB
- {
- public Matrix4x4 m44;
- public Vector3 h;
- public Vector3 c;
-
- public OBB(Matrix4x4 mat44, Vector3 hlv, Vector3 center)
- {
- m44 = mat44;
- h = hlv;
- c = center;
- }
-
- public OBB(Matrix4x4 mat44, AABB aabb)
- {
- m44 = mat44;
- h = (aabb.max - aabb.min) * 0.5f;
- c = (aabb.max + aabb.min) * 0.5f;
- }
- }
-
- public static bool Overlap_Point_AABB(Vector3 p, AABB aabb)
- {
- return ((p.x >= aabb.min.x && p.x <= aabb.max.x) && (p.y >= aabb.min.y && p.y <= aabb.max.y) && (p.z >= aabb.min.z && p.z <= aabb.max.z));
- }
-
- public static bool Overlap_Point_OBB(Vector3 p, Vector3 obbWorldPos, OBB obb)
- {
- AABB aabb = new AABB(obb.c - obb.h, obb.c + obb.h);
- Vector3 local_p = p - obbWorldPos;
- Vector3 t = obb.m44.MultiplyVector(local_p);
- return Overlap_Point_AABB(t, aabb);
- }
记住,OBB参数设置,中心一定要在世界原点,这样才方便计算
- Vector3 min = - size * 0.5f;
- Vector3 max = size * 0.5f;
- AABB aabb = new AABB(min, max);
- OBB obb = new OBB(m44, aabb);
相交和计算力方向:
- if (PhysicsUtil.Overlap_Point_OBB(script.transform.position, center, obb))
- {
-
- if (dirType == ForceDirType.Left_Right)
- {
- Vector3 dir = script.transform.position - center;
- dir = m44.MultiplyVector(dir);
- dir = (dir.x < 0) ? m44.transpose.MultiplyVector(Vector3.left) : m44.transpose.MultiplyVector(Vector3.right);
-
- Vector3 force = dir.normalized * strength;
- script.AddForce(force);
- }
- }
效果图:
1.圆形范围,力的方向从圆心到目标,模拟气浪把把草震开.
2.矩形范围,力的方向是玩家面向的左和右.模拟剑气把草劈开的感觉.
效率优化:
1.控制互动草的数量,这种草不能合批,谨记.
2.脚本加上了OnBecameVisible,OnBecameInvisible 只让摄像机内草起作用.