我在Unity中开发一款游戏,我遵循了一个关于角色移动和角色摄像头控制的短小精悍的Dino教程。一切都在处理一些小问题,其中大部分我都解决了,但有一个问题我无法解决,那就是当我将摄像头向左或向右移动90度时,角色就会失控旋转,我花了很长时间浏览评论和观看其他的视频和东西,但似乎没有任何工作。
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementController : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private float jumpForce;
[SerializeField] private float JumpraycastDistance;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
Jump();
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
float hAxis = Input.GetAxisRaw("Horizontal");
float vAxis = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.fixedDeltaTime;
Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement);
rb.MovePosition(newPosition);
}
private void Jump()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if (IsGrounded())
{
rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
}
}
}
private bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, JumpraycastDistance);
}
}
Video of broken character
任何和所有帮助非常感谢
我试了一堆我正在看的视频的youtube评论中的东西,它没有解决任何问题
摄像头代码:
[SerializeField] private float lookSensitivity;
[SerializeField] private float smoothing;
private GameObject player;
private Vector2 smoothedVelocity;
private Vector2 currentLookingPos;
private void Start()
{
player = transform.parent.gameObject;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
RotateCamera();
CheckForShooting();
}
private void RotateCamera()
{
Vector2 inputeValues = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
inputeValues = Vector2.Scale(inputeValues, new Vector2(lookSensitivity * smoothing, lookSensitivity * smoothing));
smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputeValues.x, 1f / smoothing);
smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputeValues.y, 1f / smoothing);
currentLookingPos += smoothedVelocity;
currentLookingPos.y = Mathf.Clamp(currentLookingPos.y, -80f, 80f);
transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(currentLookingPos.x, player.transform.up);
}
2条答案
按热度按时间erhoui1w1#
我在玩和研究,我发现了一个答案在Unity论坛上,它基本上解释说,我的问题被称为“万向节锁”,并修复它,你添加一个最小和最大y值.链接到后here
zsohkypk2#
我修复了我们喜欢的四元数和向量3,基本上我改变了
至