我遇到了一个问题。
我目前的基本设置是,有两个对象:我的相机和我的玩家对象。
玩家通过WASD上的Transform移动,并应该在鼠标移动时旋转。
相机是俯视的(略带“3ps”风格的角度),可以保持玩家对象在相机视角中心,并根据玩家的旋转进行旋转。
这是玩家移动脚本:
这是玩家旋转脚本:
我展示的所有内容的结果是旋转,但它没有真正与鼠标物理位置一致地旋转。当我用鼠标画圆时,它会完全旋转,但不会始终指向鼠标所在位置。我不确定为什么。
期望的结果是相机(玩家对象的子对象)跟随玩家的移动和旋转,而玩家则使用其移动脚本移动并旋转以指向鼠标所在位置。
有任何想法吗?提前感谢您。
编辑:如果有帮助的话,当前的旋转方式如下:
用鼠标在玩家周围画大圆会使旋转速度变慢,而在玩家周围画非常紧密的圆则会使旋转速度变快。
我目前的基本设置是,有两个对象:我的相机和我的玩家对象。
玩家通过WASD上的Transform移动,并应该在鼠标移动时旋转。
相机是俯视的(略带“3ps”风格的角度),可以保持玩家对象在相机视角中心,并根据玩家的旋转进行旋转。
这是玩家移动脚本:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public int playerSpeed = 8; //players movement speed
void Update () {
if (Input.GetKey ("w"))
{
transform.Translate (Vector3.forward * Time.deltaTime * playerSpeed); //move forward
}
if (Input.GetKey ("s"))
{
transform.Translate (Vector3.back * Time.deltaTime * playerSpeed); //move backwards
}
if (Input.GetKey ("a"))
{
transform.Translate (Vector3.left * Time.deltaTime * playerSpeed); //move left
}
if (Input.GetKey ("d"))
{
transform.Translate (Vector3.right * Time.deltaTime * playerSpeed); //move right
}
}
}
这是玩家旋转脚本:
using UnityEngine;
using System.Collections;
public class mouseLook : MonoBehaviour {
private Vector3 inputRotation;
private Vector3 mousePlacement;
private Vector3 screenCentre;
void Update () {
FindCrap();
transform.rotation = Quaternion.LookRotation(inputRotation);
}
void FindCrap () {
screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
mousePlacement = Input.mousePosition;
mousePlacement.z = mousePlacement.y;
mousePlacement.y = 0;
inputRotation = mousePlacement - screenCentre;
}
}
我展示的所有内容的结果是旋转,但它没有真正与鼠标物理位置一致地旋转。当我用鼠标画圆时,它会完全旋转,但不会始终指向鼠标所在位置。我不确定为什么。
期望的结果是相机(玩家对象的子对象)跟随玩家的移动和旋转,而玩家则使用其移动脚本移动并旋转以指向鼠标所在位置。
有任何想法吗?提前感谢您。
编辑:如果有帮助的话,当前的旋转方式如下:
用鼠标在玩家周围画大圆会使旋转速度变慢,而在玩家周围画非常紧密的圆则会使旋转速度变快。