gpt4 book ai didi

c# - 让玩家根据摄像机角度向前、向右、向左和向后移动

转载 作者:行者123 更新时间:2023-11-30 20:00:12 28 4
gpt4 key购买 nike

到目前为止,我有一个可以左右前后移动的玩家,但是我如何设置它是向前的,就像在网格上一样向前,而不是在我的玩家注视的地方向前。我将相机设置为 FPS,它跟随玩家。现在我想让玩家能够使用我的 MouseLook 脚本(已经工作)进行查看,但基于此我希望玩家可以看到任何地方,而不是我拥有的原始版本。我将在下面发布代码,让您知道我在说什么,感谢您的帮助。

播放器脚本(移动)

    void Update () {

if(Input.GetKey (moveU))
{
SetTransformZ((transform.position.z) + playerSpeed);

}

if(Input.GetKey (moveD))
{
SetTransformZ((transform.position.z) - playerSpeed);

}

if(Input.GetKey (moveL))
{
SetTransformX((transform.position.x) - playerSpeed);
}

if(Input.GetKey (moveR))
{
SetTransformX((transform.position.x) + playerSpeed);
}

else
{
rigidbody.angularVelocity = Vector3.zero;

}


}

void SetTransformX(float n)
{
transform.position = new Vector3(n, transform.position.y, transform.position.z);
}

void SetTransformZ(float n)
{
transform.position = new Vector3(transform.position.x, transform.position.y, n);
}

MouseLook 脚本(附加到 Unity 中的主摄像头,它与播放器一起工作,所以它会移动到我的鼠标所注视的任何地方,但移动已关闭,因为我的移动代码非常原始)

[AddComponentMenu("Camera-Control/Mouse Look")]

public class MouseLook : MonoBehaviour {

public enum RotationAxes {
MouseXAndY = 0,
MouseX = 1,
MouseY = 2 }

public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;

void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}

else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}

else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}



void Start ()
{
//if(!networkView.isMine)
//enabled = false;

// Make the rigid body not change rotation
//if (rigidbody)
//rigidbody.freezeRotation = true;

}

最佳答案

好的,所以你希望你的玩家相对于他们正在看的地方向前、向左、向右、向后移动,据我所知。

首先,确保 transform.forward 是您的播放器的正确方向。在编辑器中,您可以选择您的玩家 gameObject 并查看蓝色箭头 是否与您的玩家朝向相同的方向。如果没有,请查看 this thread在 unity answers 论坛上查看如何更改它。

这里有一些函数可以放入你的玩家 Controller 脚本中,以相对于玩家旋转而不是位置移动。(注意:查看 Time.deltaTime )

private void moveForward(float speed) {
transform.localPosition += transform.forward * speed * Time.deltaTime;
}

private void moveBack(float speed) {
transform.localPosition -= transform.forward * speed * Time.deltaTime;
}

private void moveRight(float speed) {
transform.localPosition += transform.right * speed * Time.deltaTime;
}

private void moveLeft(float speed) {
transform.localPosition -= transform.right * speed * Time.deltaTime;
}

备注:transform.position vs transform.localPosition

关于c# - 让玩家根据摄像机角度向前、向右、向左和向后移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21945082/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com