Lyn*_*rum 5 c# unity-game-engine unity5
我有一个脚本可以让你控制一个玩家并跳跃。但是,我正在努力使播放器不断移动,并且无法通过键盘上的 WASD 键进行控制。每当我尝试只使用controller.Move()重力功能时,它就会消失。现在,使用此代码 Gravity 可以工作,但 WASD 也已启用。我的问题是:我怎样才能让这段代码让我的玩家不断移动,并且仍然使用重力?
using UnityEngine;
using System.Collections;
public class PlayerMotor : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.back;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
controller.Move (Vector3.back * Time.deltaTime);
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Run Code Online (Sandbox Code Playgroud)