我正在制作一个游戏,为什么我的整个角色似乎会旋转当我移动鼠标时,有时只有我的相机移动。从我开始这个项目的那一刻我就有这个问题,但我不知怎么解决了,但它偶尔会出现,比如当我禁用我的角色移动脚本时或类似的事情。目前这个问题已经复发了,我无法控制我的角色,角色似乎会围绕一个固定的中心旋转,虽然这是我的直觉,但当我移动相机时我确实感觉到了这个问题。昨天它还在工作。并且我希望能够永久解决这个问题。
以下是有关这个问题的详细信息
- 视频显示问题(上面),当前unity版本和相关脚本。
- 首先,我的玩家层次结构如下-
https://ibb.co/pjrcvZDr "**Player"是空的游戏对象,Character是实际的游戏对象,它有脚本,Character控制器和在Character下面的主相机。 - 此外,这里是角色控制器和脚本在层次结构中的图像-
- https://ibb.co/VYMJRSwv
- https://ibb.co/FLyvXsTX
当前版本 - Unity 6.4 (6000.4.8f1)
脚本(我是一个初学者,所以脚本可能写得很糟糕)
1. 角色移动脚本-
using UnityEngine;
using UnityEngine.TextCore.Text;
public class PlayerMovement : MonoBehaviour
{
[Header("Player Settings")]
[SerializeField] float normalSpeed = 3f;
[SerializeField] float sprintSpeed = 5f;
private float speed;
//References
private CharacterController characterController;
[SerializeField] private VelocityManagement velocityManagement;
private PlayerData playerData;
[SerializeField] AudioClip grassWalking;
[SerializeField] AudioClip houseWalking;
[SerializeField] float sprintPitch = 1f;
[SerializeField] float normalPitch = 0.7f;
//Privates
float movementX;
float movementZ;
Vector3 move;
private float velocity;
private AudioSource walking;
void Start()
{
characterController = GetComponent
playerData = GetComponent
walking = GetComponent
}
void Update()
{
if (playerData.isMobile)
{
movementX = Input.GetAxisRaw("Horizontal");
movementZ = Input.GetAxisRaw("Vertical");
move = transform.forward * movementZ + transform.right * movementX;
move = move.normalized;
move.y = velocityManagement.velocity;
AudioClip walkingClip = playerData.isInside ? houseWalking : grassWalking;
walking.clip = walkingClip;
if (Input.GetKey(KeyCode.LeftShift))
{
speed = sprintSpeed;
walking.pitch = sprintPitch;
}
else
{
speed = normalSpeed;
walking.pitch = normalPitch;
}
if (movementX != 0 || movementZ != 0)
{
if (!walking.isPlaying)
{
walking.Play();
}
}
else
{
if (walking.isPlaying)
{
walking.Stop();
}
}
characterController.Move(move * speed * Time.deltaTime);
}
}
}
2. 相机旋转脚本-
using UnityEngine;
public class CameraRotation : MonoBehaviour
{
[Header("Camera Settings")]
[SerializeField] float sensitivity = 4f;
[Header("References")]
[SerializeField] Transform playerBody;
[SerializeField] private PlayerData playerData;
//Privates
private float mouseX;
private float mouseY;
private float xRotation;
private Vector3 originalLocalPosition;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
originalLocalPosition = transform.localPosition;
}
void Update()
{
//Upward/Downward Movement
if (playerData.isMobile)
{
mouseX = Input.GetAxis("Mouse X") * sensitivity;
mouseY = Input.GetAxis("Mouse Y") * sensitivity;
playerBody.rotation = Quaternion.Euler(0, playerBody.rotation.eulerAngles.y + mouseX, 0);
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
}
}
3. 速度管理脚本-
using UnityEngine;
public class VelocityManagement : MonoBehaviour
{
[Header("Settings")]
[SerializeField] float gravity = -19.6f;
//References
private CharacterController characterController;
[SerializeField] private Transform spawnPoint;
//Privates
public float velocity;
private Vector3 move;
void Start()
{
characterController = GetComponent
}
void Update()
{
if (characterController.isGrounded && velocity < 0)
{
velocity = -2f;
}
velocity += gravity * Time.deltaTime;
if (characterController.transform.position.y < -10f)
{
characterController.transform.position = spawnPoint.position + Vector3.up * characterController.height / 2;
characterController.transform.rotation = spawnPoint.rotation;
}
}
}
4. 玩家数据脚本-
using UnityEngine;
public class PlayerData : MonoBehaviour
{
[Header("Player State")]
public bool isInside = false;
public bool isMobile = true;
public bool onPC = false;
}
评论 (0)