【问题标题】:Character goes inside incline wall when moving移动时角色进入斜墙内
【发布时间】: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


【解决方案1】:

a) 在您的移动代码中,您使用 _rigidbody.position += offset; 移动刚体。这是移动物理对象的错误方式。试试吧,

rigidbody.MovePosition(rigidbody.position + offset);

b) 您可能还需要检查刚体设置,确切地说是碰撞检测模式。如果您要使用快速移动的游戏对象,则需要连续碰撞检测模式而不是离散模式。

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多