我在这里给出该内容的简体中文翻译:
我完全不知道该怎么办了。 我试了试调整玩家运动向量的方法,以使其投影在斜面法线上。 我有地面的检测和自定义重力,且不使用刚体。 我可以在斜面上跳跃并站在上面,但一旦我尝试移动时,我就会从里到外掉落,下滑能工作,但感觉像我才真正掉落一样而不是向下移动。 甚至我的碰撞系统对于小障碍物(较少)并不起作用,即使代码检测了碰撞,它不会做任何事情,感觉我一直都会透视, 我试过在我的移动向量变为零向量时碰撞,试试看它能起作用。 我也试过禁用碰撞以测试斜面运动,以防止那就是问题的根源,但它没有起作用。
//检测是否着地
isGrounded = Physics.Raycast(transform.position, Vector3.down, groundDistance, groundMask);
//检测线条是否着地
if (Physics.Linecast(previousPosition, currentPosition, out RaycastHit hit, groundMask))
{
isGrounded = true;
Debug.DrawLine(previousPosition, currentPosition, Color.green, 0.1f);
transform.position = hit.point;
}
//获取输入方向,旋转角色
//键盘和鼠标输入
if(Input.GetKey(KeyCode.W) && Input.GetMouseButton(1))
{
inputVector = cameraForward;
}
else if(Input.GetKey(KeyCode.W))
{
inputVector += transform.forward;
}
if(Input.GetKey(KeyCode.S) && Input.GetMouseButton(1))
{
inputVector = -cameraForward;
}
else if(Input.GetKey(KeyCode.S))
{
inputVector -= transform.forward;
}
if(Input.GetKey(KeyCode.E))
{
inputVector += transform.right;
}
if(Input.GetKey(KeyCode.Q))
{
inputVector -= transform.right;
}
if(Input.GetKey(KeyCode.D))
{
transform.Rotate(0, cameraRotationSpeed * Time.deltaTime, 0);
}
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(0, -cameraRotationSpeed * Time.deltaTime, 0);
}
//斜面运动
RaycastHit hit2;
if (Physics.Raycast(transform.position, Vector3.down, out hit2, 2f, groundMask))
{
//使用投影求斜面向量
Vector3 slopeMove = Vector3.ProjectOnPlane(inputVector.normalized, hit2.normal);
finalMoveVector = slopeMove * Time.deltaTime * currentSpeed;
}
else
{
finalMoveVector = inputVector.normalized * Time.deltaTime * currentSpeed;
}
Vector3 capsuleBottom = transform.position + Vector3.up * radius;
Vector3 capsuleTop = transform.position + Vector3.up * (height-radius);
//碰撞检测
if (Physics.CapsuleCast(
capsuleBottom,
capsuleTop,
radius,
finalMoveVector.normalized,
out RaycastHit hit3,
finalMoveVector.magnitude)
)
{
if (Vector3.Angle(hit3.normal, Vector3.up) > 45f)
{
float safeDistance = hit3.distance - 0.02f;
Vector3 moveToWall = finalMoveVector.normalized * safeDistance;
Vector3 slide = Vector3.ProjectOnPlane(finalMoveVector - moveToWall, hit3.normal);
finalMoveVector = moveToWall + slide;
}
//finalMoveVector = Vector3.zero;
}
//跳跃逻辑
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity = initialVelocity;
isGrounded = false;
}
else if (!isGrounded)
{
velocity += gravity * Time.deltaTime;
}
else
{
velocity = 0;
}
inputVector.y = velocity*Time.deltaTime;
finalMoveVector.y = velocity * Time.deltaTime;
Debug.Log("Hit: " + hit3.collider.name);
//转换角色
Quaternion cameraRotation = cameraTransform.rotation;
cameraRotation.x =0;
cameraRotation.z = 0;
if(Input.GetMouseButton(1) || isInCombat)
{
//transform.forward = Vector3.Slerp(transform.forward, -normCameraDirection, Time.deltaTime*rotationSpeed);
transform.rotation = cameraRotation;
}
transform.position += finalMoveVector;
这不是整个代码,我也有一个调整速度的部分但那不是非常重要的。 这是所有内容都在 Update 函数里面。 有关此问题,有任何建议吗?
评论 (0)