[英]Mouse axes are based on position, not movement
基本上,当我将光标放在屏幕中央时,相机不会旋转。 很标准。 但是,如果我向左或向右移动光标,它将开始旋转。 还是有道理的。 但是,当我停止移动光标时,旋转继续。 我正在使用一个空的对象,并使相机面向与该空的对象相同的方向。 问题是,对象继续旋转。 如果我将光标移回屏幕中心,它将再次停止旋转。 当我将项目设置中的轴分配给Xbox 1控制器上的右摇杆时,也会发生类似的情况。 如果我向右移动操纵杆,相机将开始旋转,但是,如果我将操纵杆放回死区,它将继续旋转。 如果我再向左移动摇杆,它将减慢旋转速度,并最终开始向另一个方向旋转。 但是,垂直杆轴不会发生这种情况。
这是我的播放器鼠标和空对象旋转的代码:
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public Transform PlayerT;
public Transform camT;
public Transform CamR;
public CharacterController controller;
public float speed;
public float CamRSpeed;
private float gravity;
private float HorizontalR;
private float VerticalR;
private Vector3 moveDirection;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
HorizontalR = Input.GetAxis("Mouse X") * CamRSpeed * Time.deltaTime;
VerticalR = Input.GetAxis("Mouse Y") * CamRSpeed * Time.deltaTime;
CamR.Rotate(VerticalR, HorizontalR, 0f);
PlayerT.forward = CamR.forward;
PlayerT.eulerAngles = new Vector3(0f, PlayerT.eulerAngles.y, 0f);
moveDirection = (PlayerT.forward * Input.GetAxis("Vertical") * speed) + (PlayerT.right * Input.GetAxis("Horizontal") * speed);
controller.Move(moveDirection * Time.deltaTime);
}
}
//and for the camera (this is a separate script, I'm just not entirely sure how this site's formatting works):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
{
public Transform cam;
public Transform player;
public Transform CamR;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = CamR.position - cam.position;
}
// Update is called once per frame
void Update()
{
cam.eulerAngles = new Vector3(CamR.eulerAngles.x, CamR.eulerAngles.y, 0f);
cam.position = CamR.position - (CamR.rotation * offset);
}
}```
好吧,事实证明,这仅仅是因为我尝试旋转的对象是玩家的孩子。 我不完全确定为什么它不起作用,但现在可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.