求unity3d 用wasd与空格键控制人物移动的代码。
2个回答
展开全部
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);
}
}
这个够简单吧。。。- -!
记得限定一下移动范围
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);
}
}
这个够简单吧。。。- -!
记得限定一下移动范围
展开全部
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;
}
}
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询