求unity3d 用wasd与空格键控制人物移动的代码。

如题所述

public float MoveSpeed = 1.0f;

void Update ()
{
if (Input.GetKey (KeyCode.W))
{
transform.Translate(Vector3.up * Time.deltaTime * MoveSpeed);
}

if (Input.GetKey (KeyCode.S))
{
transform.Translate(Vector3.down * Time.deltaTime * MoveSpeed);
}

if (Input.GetKey (KeyCode.A))
{
transform.Translate(Vector3.left * Time.deltaTime * MoveSpeed);
}

if (Input.GetKey (KeyCode.D))
{
transform.Translate(Vector3.right * Time.deltaTime * MoveSpeed);
}
}

这个够简单吧。。。- -!
记得限定一下移动范围
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-11-18
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zhurenwu : MonoBehaviour
{
float sudu = 0.01f;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 position = transform.position;
position.x = position.x + sudu * horizontal;
position.z = position.z + sudu * vertical;

if (Input.GetButtonDown("Jump")
{
position.y = position.y + 0.3f;
}
transform.position = position;
}
}
相似回答