简体   繁体   English

Unity 中的第一人称动作 3d

[英]first person movement in Unity 3d

I'm petty new to unity and need to know how to make my player move in the way it's looking using this code我对统一有点陌生,需要知道如何让我的玩家按照使用此代码的方式移动

For example: I want it to move in the way that my player camera is facing when I press w例如:我希望它在我按下 w 时以我的播放器相机面对的方式移动

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

public class PlayerMovement2 : MonoBehaviour
{
    public Rigidbody rb;

    public float speed;

    void FixedUpdate()
    {
        if (Input.GetKey("w"))
        {
            Vector3 input2 = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
            rb.MovePosition(transform.position + input2 * speed * Time.deltaTime);
        }
        else if (Input.GetKey("s"))
        {
            Vector3 input3 = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
            rb.MovePosition(transform.position + input3 * speed * Time.deltaTime * -1);
        }
        else if (Input.GetKey("a") || Input.GetKey("d"))
        {
            Vector3 input1 = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            rb.MovePosition(transform.position + input1 * speed * Time.deltaTime);
        }
    }
}

Since you are new to unity, I would recommend you to watch Brackeys tutorial on player movement.由于您是 Unity 新手,我建议您观看 Brackeys 关于玩家移动的教程。 It gives a detailed answer to your question.它为您的问题提供了详细的答案。

First person: https://youtu.be/_QajrabyTJc第一人称: https://youtu.be/_QajrabyTJc

Third person: https://youtu.be/4HpC--2iowE第三人称: https://youtu.be/4HpC--2iowE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM