一、WASD键控制物体前后左右移动一定的距离
void myMove()
{
if(Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
transform.Translate(-0.8f, 0, 0);
}
if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.Translate(0.8f, 0, 0);
}
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.DownArrow))
{
transform.Translate(0, 0, -0.8f);
}
if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.UpArrow))
{
transform.Translate(0, 0, 0.8f);
}
二、WASD控制物体连续移动 [连续平滑移动]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
///
/// </summary>
public class Xiao_Move : MonoBehaviour //脚本名应该和自己的脚本的名字一致
{
public float speed = 5;
private float h, v;
void moveText()//控制移动的幻函数
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
transform.Translate(new Vector3(h, -v, 0) * speed * Time.deltaTime);
}
void Start()
{
}
void Update()
{
moveText();
}
}
标签:0.8,GetKeyDown,transform,几种,Unity,Input,平面,Translate,KeyCode From: https://www.cnblogs.com/HanaKoo/p/16813719.html