【发布时间】:2021-08-27 00:31:28
【问题描述】:
我写了角色控制器脚本,通过改变rigidbody.position来实现移动,当我的角色尝试靠近墙壁向前移动时,他有点陷入倾斜的墙壁和地板。Character already in another collider ( i held right arrow)
我尝试进行投射并检查玩家对撞机是否在他移动时发生碰撞,但我认为这不好解决我的问题,因为如果我想按角色身体等移动项目会产生问题。
我认为可以通过统一刚体设置或一些代码来解决问题
我的动作代码:
public void Move(Vector2 direction)
{
Vector2 directionAlongSurface = _surfaceSlider.GetDirection(direction.normalized); // It get direction if player try walk on a slope (forward - Vector2.Dot(forward, _normal) * _normal;)
Vector2 offset = directionAlongSurface * (_speed * Time.deltaTime);
//var colliders = Physics2D.BoxCastAll(_boxCollider.transform.position, _boxCollider.size, 90, offset, offset.magnitude,_layerMask);
//if(colliders.Length == 0) //
_rigidbody.position += offset;
}
P.S 字符碰撞器即使在平坦的墙壁中也会进入内部
【问题讨论】:
-
在3D场景中,我们通常会在角色上添加RigidBody和Mesh Collider(其他类型的colliders也可以),我们同时添加不同类型的colliders(取决于目标的形状和效率) 在地面或墙壁等其他场景对象上。然后我们就轻松地让 Unity 处理碰撞和其他物理效果。我希望这会对你有所帮助。
标签: c# unity3d game-physics