相机相对运动
Camera Relative Movement
我 运行 遇到了一个我无法完全解决的问题。
我正在通过 Unity 制作一个俯视视角的 3D 游戏。我有一个控制器,可以让我的角色在按下移动键时旋转和移动。例如按 "D" 会将播放器对象旋转到面向右,然后 rigidBody 朝那个方向移动,但是如果我旋转相机,播放器将不会相对于相机方向向右移动。
运动:
//Player Movement
void HandleMove()
{
if (canMove == true)
{
//Detect input movement
var moveHorizontal = Input.GetAxis("Horizontal");
var moveVertical = Input.GetAxis("Vertical");
IsMoving = moveHorizontal != 0 || moveVertical != 0;
Vector3 relative = transform.rotation.eulerAngles;
relative.y = pCamera.playerRelativeAngle;
//Rotate the character
var movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
var rot = movement * (speed / 10);
if (attackTimer <= 0 && movement != Vector3.zero)
{
Vector3 temp = transform.rotation.eulerAngles;
var newRotation = Quaternion.LookRotation(rot);
transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, 10F);
}
//Move the character
if (IsMoving && offset.y != 0f)
{
movement.y = offset.normalized.y * movement.magnitude;
}
var characterMovement = transform.position + movement;
if (attackTimer <= 0 || !IsGrounded)
{
rbody.MovePosition(characterMovement);
}
}
我对要做什么有一点想法,但我不能完全掌握它,我有一个相机设置可以 return 它的欧拉角,我正试图找出一种使用它的方法用于相对运动,但我不确定在我的运动控制器中在哪里或如何应用它。
相机:
//Find camera angle
public static float playerRelativeAngle;
//On awake, do this
void Awake()
{
//Get the cameras angle so we can move the player relative to it???
playerRelativeAngle = this.transform.localEulerAngles.y;
}
//Update every frame
void Update()
{
playerRelativeAngle = this.transform.localEulerAngles.y;
//If the right key is pressed then the caerma rotates to the right
if (Input.GetKey("right"))
{
this.transform.RotateAround(target.transform.position, Vector3.down, cameraSpeed * Time.deltaTime);
}
if (Input.GetKey("left"))
{
this.transform.RotateAround(target.transform.position, Vector3.down, -cameraSpeed * Time.deltaTime);
}
}
非常感谢任何建议。
我设法找到了我的问题的解决方案。
运动:
//Rotate the character relative to the camera direction
if (attackTimer <= 0 && moveDirection != Vector3.zero)
{
var targetDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
targetDirection = Camera.main.transform.TransformDirection(targetDirection);
targetDirection.y = 0.0f;
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
float rotationSpeed = 100.0f;
Quaternion newRotation = Quaternion.Lerp(rbody.rotation, targetRotation, rotationSpeed * Time.deltaTime);
//Apply the rotation
rbody.MoveRotation(newRotation);
}
//Move the character
if (IsMoving == true)
{
if (Input.GetAxis("Vertical") != 0) //Vertical movement (Up/Down)
{
if (attackTimer <= 0 || !IsGrounded)
{
rbody.AddForce(transform.forward * speed);
}
}
玩家对象现在相对于相机旋转,然后在按下移动键时向玩家向前方向施加力。
这不是一个完美的解决方案,但希望这对将来的人有所帮助。
我 运行 遇到了一个我无法完全解决的问题。
我正在通过 Unity 制作一个俯视视角的 3D 游戏。我有一个控制器,可以让我的角色在按下移动键时旋转和移动。例如按 "D" 会将播放器对象旋转到面向右,然后 rigidBody 朝那个方向移动,但是如果我旋转相机,播放器将不会相对于相机方向向右移动。
运动:
//Player Movement
void HandleMove()
{
if (canMove == true)
{
//Detect input movement
var moveHorizontal = Input.GetAxis("Horizontal");
var moveVertical = Input.GetAxis("Vertical");
IsMoving = moveHorizontal != 0 || moveVertical != 0;
Vector3 relative = transform.rotation.eulerAngles;
relative.y = pCamera.playerRelativeAngle;
//Rotate the character
var movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
var rot = movement * (speed / 10);
if (attackTimer <= 0 && movement != Vector3.zero)
{
Vector3 temp = transform.rotation.eulerAngles;
var newRotation = Quaternion.LookRotation(rot);
transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, 10F);
}
//Move the character
if (IsMoving && offset.y != 0f)
{
movement.y = offset.normalized.y * movement.magnitude;
}
var characterMovement = transform.position + movement;
if (attackTimer <= 0 || !IsGrounded)
{
rbody.MovePosition(characterMovement);
}
}
我对要做什么有一点想法,但我不能完全掌握它,我有一个相机设置可以 return 它的欧拉角,我正试图找出一种使用它的方法用于相对运动,但我不确定在我的运动控制器中在哪里或如何应用它。
相机:
//Find camera angle
public static float playerRelativeAngle;
//On awake, do this
void Awake()
{
//Get the cameras angle so we can move the player relative to it???
playerRelativeAngle = this.transform.localEulerAngles.y;
}
//Update every frame
void Update()
{
playerRelativeAngle = this.transform.localEulerAngles.y;
//If the right key is pressed then the caerma rotates to the right
if (Input.GetKey("right"))
{
this.transform.RotateAround(target.transform.position, Vector3.down, cameraSpeed * Time.deltaTime);
}
if (Input.GetKey("left"))
{
this.transform.RotateAround(target.transform.position, Vector3.down, -cameraSpeed * Time.deltaTime);
}
}
非常感谢任何建议。
我设法找到了我的问题的解决方案。
运动:
//Rotate the character relative to the camera direction
if (attackTimer <= 0 && moveDirection != Vector3.zero)
{
var targetDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
targetDirection = Camera.main.transform.TransformDirection(targetDirection);
targetDirection.y = 0.0f;
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
float rotationSpeed = 100.0f;
Quaternion newRotation = Quaternion.Lerp(rbody.rotation, targetRotation, rotationSpeed * Time.deltaTime);
//Apply the rotation
rbody.MoveRotation(newRotation);
}
//Move the character
if (IsMoving == true)
{
if (Input.GetAxis("Vertical") != 0) //Vertical movement (Up/Down)
{
if (attackTimer <= 0 || !IsGrounded)
{
rbody.AddForce(transform.forward * speed);
}
}
玩家对象现在相对于相机旋转,然后在按下移动键时向玩家向前方向施加力。
这不是一个完美的解决方案,但希望这对将来的人有所帮助。