繁体   English   中英

如何让玩家在 3D 空间中移动?

[英]How to make player move in 3D space?

我想将我的播放器移动到 3D 空间。 我可以使用以下代码使其上下移动:

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

    public class Player : MonoBehaviour
    {
        private CharacterController _controller;


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


        void Update()
        {
            float horizontalInput = Input.GetAxis("Horizontal");
            float verticalInput = Input.GetAxis("Vertical");
            Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
            _controller.Move(direction * Time.deltaTime);
        }
    }

如何使播放器在 z 轴上移动?

如何使用VerticalHorizontal轴在 X 和 Z 上移动CharacterController ,然后使用另一个输入(按钮或轴)在 Y 轴上垂直移动它? 这是一个很好的起点。

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

public class Player: MonoBehaviour
{
   private CharacterController cc;


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


   void Update()
   {
      float horizontal = Input.GetAxis("Horizontal");
      float vertical = Input.GetAxis("Vertical");

      Vector3 direction = new Vector3(horizontal, 0f, vertical);

      if (Input.GetKeyDown(KeyCode.Space)) direction.y = 5f;

      // Simulate some kind of gravity
      direction.y -= 9.81 f * Time.deltaTime;

      cc.Move(direction * Time.deltaTime);
   }
}

暂无
暂无

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

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