我正在开发一个主要集中在户外运动的游戏,因此我希望角色控制感觉尽可能好。
我目前正在研究的问题是斜坡的行为:当你站在不太陡峭的斜坡上时,这个角色不应该滑落,而在太陡的斜坡上滑行。
我通过激活和解除刚体约束来实现这一点,这取决于玩家下面地面的当前角度。
private const RigidbodyConstraints DefaultConstraints = RigidbodyConstraints.FreezeRotation;
private const RigidbodyConstraints StayOnSlope = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | DefaultConstraints;
private const RigidbodyConstraints SlideDownSlope = DefaultConstraints;用分离法计算地面的角度,以度为单位返回向上矢量与地面法线之间的夹角。
private float GetGroundAngle()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 0.5f))
{
return Vector3.Angle(Vector3.up, hit.normal);
}
return 0;
}约束的实际激活和失活在FixedUpdate方法中实现。另外,玩家的移动速度越慢,坡度越陡。
private void FixedUpdate()
{
const float MAX_SLOPE_ANGLE = 45;
// If the player is grounded, check the ground angle and prevent slope sliding
float angle = GetGroundAngle();
// Apply the constraints
m_rigidbody.constraints = (m_movementVector.magnitude < Vector3.kEpsilon) && angle <= MAX_SLOPE_ANGLE ? StayOnSlope : SlideDownSlope;
// Calculate the movement coefficient to ensure the player cannot run up slopes
float slopeCoefficient = Mathf.Cos(angle * Mathf.Deg2Rad);
// Calculate and apply the movement vector
Vector3 movement = m_movementVector * slopeCoefficient * Time.fixedDeltaTime;
m_rigidbody.MovePosition(m_rigidbody.position + movement);
// ...
}此功能的问题如下:
有没有更好的方法使斜坡运动的行为?
发布于 2018-04-22 17:04:09
当角色站在不太陡峭的斜坡上时,不应该滑下去,而在太陡的斜坡上滑行。
我相信CharacterController组件可能对你有用。
注意,它有一个可调整的斜率限制变量。
https://docs.unity3d.com/Manual/class-CharacterController.html
在under资产中有第一个实现此功能的控制器,它可以在资产->导入包->字符中找到。
https://stackoverflow.com/questions/49959324
复制相似问题