如何解决角色旋转时Cinemachine的Unity问题
我正在尝试使用Rigidbody和Cinemachine实现3D,第三人称角色移动。 角色应该能够在地面和天花板上以及墙壁上行走(面向正X轴时只能在左右墙壁上行走)。
我通过在刚体上增加/改变恒定力来改变重力方向,并在x上向上和向下旋转字符,在z上向右和左旋转字符来实现这一点。
CinemachineBrain的Characters转换设置了World Up Override,而CinemchineFreeLook绑定模式设置为“ World Up的简单跟进”。这使摄影机/电影机的轨道相对于角色旋转而旋转。
到目前为止,我已经在PlayerMovement脚本中实现了4种方法,其中一种针对每个重力方向(上,下,左,右),每个方法在改变旋转和重力的情况下基本相同。
MoveUp()和MoveDown()方法按预期工作。
但是我无法使用MoveLeft()和MoveRight()方法。
问题在于,添加摄影机角度时,角色无法进行360度旋转(在x轴上),而面对正x或负x时角色只能旋转180度(感觉像是看不见的墙)在y轴上。
在某种程度上,我希望Cinemachine / + Camera.main.transform.eulerAngles.x;
会成为问题,但是我无法弄清楚到底是什么。
这是我总体上第一个Unity /游戏开发项目-所以我希望我能提供所有必要的信息。
感谢答案。
这是我的脚本:
(我按照本教程实施了基本动作,并将其更改为与刚体一起使用:https://youtu.be/4HpC--2iowE)
public class PlayerMovement : MonoBehavIoUr
{
public float walkSpeed = 5f;
public float runSpeed = 15f;
public float idleSpeed = 0f;
public string gravityDiretion = "down";
public ConstantForce gravity = new ConstantForce();
private float actualSpeed;
public float turnSmoothing = 0.1f;
float turnSmoothVeLocity;
private Rigidbody character;
private void Start()
{
character = GetComponent<Rigidbody>();
gravity = gameObject.AddComponent<ConstantForce>();
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
if (gravityDiretion.Equals("up"))
{
MoveUp();
}
else if (gravityDiretion.Equals("down"))
{
MoveDown();
}
else if (gravityDiretion.Equals("right"))
{
MoveRight();
}
else if (gravityDiretion.Equals("left"))
{
MoveLeft();
}
}
private void MoveUp()
{
gravity.force = new Vector3(0.0f,9.81f,0.0f);
if (Input.GetKey(KeyCode.LeftShift))
{
actualSpeed = runSpeed;
}
else
{
actualSpeed = walkSpeed;
}
float horizonal = Input.GetAxisRaw("Horizontal"); // 1 = left -1 righ
float vertical = Input.GetAxisRaw("Vertical"); // 1 = forward -1 bakwadrd
Debug.Log("Axis Vertical = " + vertical);
Debug.Log("Axis Horizontal = " + horizonal);
Vector3 direction = new Vector3(horizonal,0f,-vertical).normalized; //normalize to not moving faster when pressing two keys to walk diagonal
if (direction.magnitude >= 0.1f) // If lenght of the vecctor is > 0
{
//Character Rotation
float targetAngle = Mathf.atan2(direction.x,direction.z) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.y; // The angle the character should turn to face forward
Debug.Log("targetAngle = " + targetAngle);
float smoothedAngle = Mathf.SmoothdampAngle(transform.eulerAngles.y,targetAngle,ref turnSmoothVeLocity,- turnSmoothing);
Debug.Log("smoothedAngle = " + smoothedAngle);
character.MoveRotation(Quaternion.Euler(180f,smoothedAngle,0f));
//Character Movement
Vector3 moveDir = Quaternion.Euler(180f,0f) * Vector3.forward;
Debug.Log("Movedir = " + moveDir.ToString());
character.MovePosition(transform.position + moveDir.normalized * actualSpeed * Time.fixedDeltaTime);
}
else
{
actualSpeed = 0f;
}
}
private void MoveDown()
{
//Gravity
gravity.force = new Vector3(0.0f,-9.81f,0.0f);
if (Input.GetKey(KeyCode.LeftShift))
{
actualSpeed = runSpeed;
}
else
{
actualSpeed = walkSpeed;
}
float horizonal = Input.GetAxisRaw("Horizontal"); // 1 = left -1 righ
float vertical = Input.GetAxisRaw("Vertical"); // 1 = forward -1 bakward
Debug.Log("Axis Vertical = " + vertical);
Debug.Log("Axis Horizontal = " + horizonal);
Vector3 direction = new Vector3(horizonal,vertical).normalized;
if (direction.magnitude >= 0.1f)
{
//Character Rotation
float targetAngle = Mathf.atan2(direction.x,direction.z) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.y;
float smoothedAngle = Mathf.SmoothdampAngle(transform.eulerAngles.y,turnSmoothing);
character.MoveRotation(Quaternion.Euler(0f,0f));
//Character Movement
Vector3 moveDir = Quaternion.Euler(0f,0f) * Vector3.forward;
Debug.Log("Movedir = " + moveDir.ToString());
character.MovePosition(transform.position + moveDir.normalized * actualSpeed * Time.fixedDeltaTime);
}
else
{
actualSpeed = 0f;
}
}
private void MoveRight()
{
//Gravity
gravity.force = new Vector3(9.81f,0.0f,0.0f);
if (Input.GetKey(KeyCode.LeftShift))
{
actualSpeed = runSpeed;
}
else
{
actualSpeed = walkSpeed;
}
float horizonal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Debug.Log("Axis Vertical = " + vertical);
Debug.Log("Axis Horizontal = " + horizonal);
Vector3 direction = new Vector3( 0f,horizonal,vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.atan2(direction.y,direction.z) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.x;
Debug.Log("targetAngle = " + targetAngle);
float smoothedAngle = Mathf.SmoothdampAngle(transform.eulerAngles.x,turnSmoothing);
Debug.Log("smoothedAngle = " + smoothedAngle);
character.MoveRotation( Quaternion.Euler(targetAngle,90f));
//Character Movement
Vector3 moveDir = Quaternion.Euler(targetAngle,90f) * Vector3.forward;
Debug.Log("Movedir = " + moveDir.ToString());
character.MovePosition(transform.position + moveDir.normalized * actualSpeed * Time.fixedDeltaTime);
}
else
{
actualSpeed = 0f;
}
}
private void MoveLeft()
{
//Gravity
gravity.force = new Vector3(-9.81f,0.0f);
if (Input.GetKey(KeyCode.LeftShift))
{
actualSpeed = runSpeed;
}
else
{
actualSpeed = walkSpeed;
}
float horizonal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Debug.Log("Axis Vertical = " + vertical);
Debug.Log("Axis Horizontal = " + horizonal);
Vector3 direction = new Vector3(0f,vertical).normalized;
if (direction.magnitude >= 0.1f)
{
//Character Rotation
float targetAngle = Mathf.atan2(direction.z,direction.y) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.x;
float smoothedAngle = Mathf.SmoothdampAngle(transform.eulerAngles.x,turnSmoothing);
character.MoveRotation(Quaternion.Euler(smoothedAngle,-90f));
//Character Movement
Vector3 moveDir = Quaternion.Euler(targetAngle,-90f) * Vector3.right;
Debug.Log("Movedir = " + moveDir.ToString());
character.MovePosition(transform.position + moveDir.normalized * actualSpeed * Time.fixedDeltaTime);
}
else
{
actualSpeed = 0f;
}
}
public void ChangeDravityDirection(string newGravityDirection)
{
gravityDiretion = newGravityDirection;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。