移动 object 和 _camera.TransformDirection(moveDirection)
move object and _camera.TransformDirection(moveDirection)
我正在使用这个简单的功能移动游戏 object,移动 body 取决于相机视图。唯一的问题是相机 X 旋转不是 0 并且 body 试图移动到地面但不能向前清除并且移动缓慢
void VerySimpleMove()
{
if (_controllerBody.isGrounded)
{
_moveDirection.x = _mj.GetAxis("Horizontal");
_moveDirection.y = 0;
_moveDirection.z = _mj.GetAxis("Vertical");
_moveDirection = _camera.TransformDirection(_moveDirection);
if (Mathf.Abs(_moveDirection.x) > 0 || Mathf.Abs(_moveDirection.y) > 0)
{
_body.rotation = Quaternion.LookRotation(_moveDirection);
}
if (_jumpButton)
{
_jumpButton = false;
_moveDirection.y = _jumpHeight;
}
}
_moveDirection.y -= _gravity * Time.deltaTime;
_controllerBody.Move(_moveDirection * Time.deltaTime);
}
如何将 _moveDirection 旋转 _camera.eulerAngle.x 使其真正向前?
如果我没理解错的话,你需要确保_moveDirection总是平行于XZ平面。
一种方法是不使用 _camera.TransformDirection
来计算相机前向矢量在 XZ 平面上的投影,然后在该方向使用 LookRotation
,如下所示:
var forward = _camera.transform.forward; // Get camera forward vector
forward.y = 0; // Project it on XZ plane
_moveDirection = Quaternion.LookRotation(forward) * _moveDirection; // Rotate
请注意,简单地按建议的相机 X 角度旋转对象并不总是有用,因为相机 Z 角度会产生相同的效果。
所以最终代码是:
void VerySimpleMove()
{
if (_controllerBody.isGrounded)
{
_moveDirection.x = _mj.GetAxis("Horizontal");
_moveDirection.y = 0;
_moveDirection.z = _mj.GetAxis("Vertical");
var forward = _camera.transform.forward; // Get camera forward vector
forward.y = 0; // Project it on XZ plane
_moveDirection = Quaternion.LookRotation(forward) * _moveDirection; // Rotate
if (Mathf.Abs(_moveDirection.x) > 0 || Mathf.Abs(_moveDirection.y) > 0)
{
_body.rotation = Quaternion.LookRotation(_moveDirection);
}
if (_jumpButton)
{
_jumpButton = false;
_moveDirection.y = _jumpHeight;
}
}
_moveDirection.y -= _gravity * Time.deltaTime;
_controllerBody.Move(_moveDirection * Time.deltaTime);
}
我正在使用这个简单的功能移动游戏 object,移动 body 取决于相机视图。唯一的问题是相机 X 旋转不是 0 并且 body 试图移动到地面但不能向前清除并且移动缓慢
void VerySimpleMove()
{
if (_controllerBody.isGrounded)
{
_moveDirection.x = _mj.GetAxis("Horizontal");
_moveDirection.y = 0;
_moveDirection.z = _mj.GetAxis("Vertical");
_moveDirection = _camera.TransformDirection(_moveDirection);
if (Mathf.Abs(_moveDirection.x) > 0 || Mathf.Abs(_moveDirection.y) > 0)
{
_body.rotation = Quaternion.LookRotation(_moveDirection);
}
if (_jumpButton)
{
_jumpButton = false;
_moveDirection.y = _jumpHeight;
}
}
_moveDirection.y -= _gravity * Time.deltaTime;
_controllerBody.Move(_moveDirection * Time.deltaTime);
}
如何将 _moveDirection 旋转 _camera.eulerAngle.x 使其真正向前?
如果我没理解错的话,你需要确保_moveDirection总是平行于XZ平面。
一种方法是不使用 _camera.TransformDirection
来计算相机前向矢量在 XZ 平面上的投影,然后在该方向使用 LookRotation
,如下所示:
var forward = _camera.transform.forward; // Get camera forward vector
forward.y = 0; // Project it on XZ plane
_moveDirection = Quaternion.LookRotation(forward) * _moveDirection; // Rotate
请注意,简单地按建议的相机 X 角度旋转对象并不总是有用,因为相机 Z 角度会产生相同的效果。
所以最终代码是:
void VerySimpleMove()
{
if (_controllerBody.isGrounded)
{
_moveDirection.x = _mj.GetAxis("Horizontal");
_moveDirection.y = 0;
_moveDirection.z = _mj.GetAxis("Vertical");
var forward = _camera.transform.forward; // Get camera forward vector
forward.y = 0; // Project it on XZ plane
_moveDirection = Quaternion.LookRotation(forward) * _moveDirection; // Rotate
if (Mathf.Abs(_moveDirection.x) > 0 || Mathf.Abs(_moveDirection.y) > 0)
{
_body.rotation = Quaternion.LookRotation(_moveDirection);
}
if (_jumpButton)
{
_jumpButton = false;
_moveDirection.y = _jumpHeight;
}
}
_moveDirection.y -= _gravity * Time.deltaTime;
_controllerBody.Move(_moveDirection * Time.deltaTime);
}