如何解决Unity C#相机故障
我正在制作FPS游戏,当我直接向左或向右看时,我的Z旋转设置为90,并且我的角色(胶囊)快速旋转。自从我将照相机脚本添加到程序以来,这种情况就一直在发生,所以我认为添加后的任何内容都与它无关。
这是我的“相机运动”代码:
beam.io.WriteToTFRecord
这是我胶囊运动的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCameraController : MonoBehaviour
{
[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 = true;
}
private void Update()
{
RotateCamera();
}
private void RotateCamera()
{
Vector2 inputValues = new Vector2(Input.GetAxisRaw("Mouse X"),Input.GetAxisRaw("Mouse Y"));
inputValues = Vector2.Scale(inputValues,new Vector2(lookSensitivity * smoothing,lookSensitivity * smoothing));
smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x,inputValues.x,1f / smoothing);
smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y,inputValues.y,1f / smoothing);
currentLookingPos += smoothedVelocity;
transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y,Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(currentLookingPos.x,player.transform.up);
}
}
这也是我的射击程序的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : 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,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,ForceMode.Impulse);
}
}
}
private bool IsGrounded()
{
return Physics.Raycast(transform.position,Vector3.down,jumpRaycastDistance);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。