[英]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.