C# 请帮我学Unity3D

C# 请帮我学Unity3D,c#,unity3d,C#,Unity3d,我是unity的新手,我希望玩家朝着摄像机的方向移动,我找不到任何有效的答案 我的动作代码在这里 using System.Collections; using System.Collections.Generic; using UnityEngine; public class movement : MonoBehaviour { private float speed = 50000; private Rigidbody rb = null; void Start

我是unity的新手,我希望玩家朝着摄像机的方向移动,我找不到任何有效的答案

我的动作代码在这里

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
    private float speed = 50000;
    

    private Rigidbody rb = null;
void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
   
}


void Update()
{
    float xSpeed = Input.GetAxis("Horizontal") * speed;
    float ySpeed = Input.GetAxis("Vertical") * speed;
    Rigidbody rb = GetComponent<Rigidbody>();
    rb.AddTorque(new Vector3(xSpeed, 0, ySpeed) * speed * Time.deltaTime);

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameera : MonoBehaviour
{
     public float mouseSens = 2f;
     float pitch;
     float yaw;
     public Transform target;
     public float dst = 2f;
     public Vector2 pitchMinMax = new Vector2(-80, 80);



// Update is called once per frame
void LateUpdate()
{
    yaw += Input.GetAxis("Mouse X") * mouseSens;
    pitch -= Input.GetAxis("Mouse Y") * mouseSens;
    pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
    Vector3 targetRotation = new Vector3(pitch, yaw);
    transform.eulerAngles = targetRotation;
    transform.position = target.position - transform.forward * dst;
}
}


请帮助我,我是Unity3D的新手。

你可以这样做,你可以通过transform.forward获得相对于空间的任何前进方向

//rb.AddTorque(new Vector3(xSpeed, 0, ySpeed) * speed * Time.deltaTime);
rb.AddTorque(Camera.main.transform.forward * speed * Time.deltaTime);

因此,获取相机的参考,并使用camera.transform.forward沿同一方向移动?您能举个例子吗?这是可行的,但我想按住W,然后沿相机的方向移动。现在一直都是这样,你需要把它和值相乘,或者检查它,如果有输入,我只是给了这个例子一个方向。