Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 玩家移动脚本,如FPS、Unity3d中的_C#_Unity3d - Fatal编程技术网

C# 玩家移动脚本,如FPS、Unity3d中的

C# 玩家移动脚本,如FPS、Unity3d中的,c#,unity3d,C#,Unity3d,我有两个脚本,使玩家移动像在FPS游戏。但它不会移动到玩家正在看的那个方向——它总是移动到同一个方向,而不管摄像机的方向 mouselook.cs float yRotation; float xRotation; float lookSensitivity = 5; float currentXRotation; float currentYRotation; float yRotationV; float xRotationV; float lookSmoothnes = 0.1f; v

我有两个脚本,使玩家移动像在FPS游戏。但它不会移动到玩家正在看的那个方向——它总是移动到同一个方向,而不管摄像机的方向

mouselook.cs

float yRotation;
float xRotation;
float lookSensitivity = 5;
float currentXRotation;
float currentYRotation;
float yRotationV;
float xRotationV;
float lookSmoothnes = 0.1f; 

void Update ()
{
    yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
    xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
    xRotation = Mathf.Clamp(xRotation, -80, 100);
    currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothnes);
    currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothnes);
    transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
}
public float walkSpeed = 6.0F;
public float jumpSpeed = 8.0F;
public float runSpeed = 8.0F;
public float gravity = 20.0F;

private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}

void Update()
{
    if (controller.isGrounded)
    {
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= walkSpeed;
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}
playermovement.cs

float yRotation;
float xRotation;
float lookSensitivity = 5;
float currentXRotation;
float currentYRotation;
float yRotationV;
float xRotationV;
float lookSmoothnes = 0.1f; 

void Update ()
{
    yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
    xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
    xRotation = Mathf.Clamp(xRotation, -80, 100);
    currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothnes);
    currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothnes);
    transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
}
public float walkSpeed = 6.0F;
public float jumpSpeed = 8.0F;
public float runSpeed = 8.0F;
public float gravity = 20.0F;

private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}

void Update()
{
    if (controller.isGrounded)
    {
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= walkSpeed;
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}
public float walkSpeed=6.0F;
公共浮子跳跃速度=8.0F;
公共浮子运行速度=8.0F;
公共浮子重力=20.0F;
专用矢量3移动方向=矢量3.0;
专用字符控制器;
void Start()
{
控制器=GetComponent();
}
无效更新()
{
if(controller.isground)
{
moveDirection=新矢量3(Input.GetAxis(“水平”),0,Input.GetAxis(“垂直”);
moveDirection=transform.TransformDirection(moveDirection);
移动方向*=行走速度;
if(Input.GetButton(“跳转”))
移动方向。y=跳跃速度;
}
moveDirection.y-=重力*时间增量;
控制器移动(移动方向*时间增量);
}

难道不需要像RequiredComponentTypeof这样的东西吗

让你的鼠标看起来像这样

float lookSensitivity = 5;
float mouseX,mouseY;
public Transform playerBody;
float xRotation = 0f;

void Update ()
{
    float mouseX = Input.GetAxis("Mouse X") * mouseSentivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSentivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

        playerBody.Rotate(Vector3.up * mouseX);

}

保存此内容,并将播放器从层次结构拖到摄影机脚本playerBody。

是否尝试使用lateUpdate()进行playermovement。cs@BurakKarasoy现在我试着用它来代替
Update
,但它没有帮助。我会尝试在playermovement.cs函数中创建一个函数,并在这里携带更新中的所有代码,然后调用mouselook.cs的更新函数的末尾,以了解这两个脚本的同步之间是否存在问题。您的两个脚本是否都连接到角色游戏对象?我试着重现你的问题,但效果很好。@ziwert这是问题所在,谢谢。请用你的答案进一步澄清