In unity development, we often use buttons to implement certain functions, such as wasd to control the movement of characters, how to achieve it? code show as below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KeysMove : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
control();
}
public void control()
{
if (Input.GetKey(KeyCode.W))
{
this.transform.Translate(Vector3.forward);
//gameObject.transform.Translate(0, 0, move, Space.Self);
}
else if (Input.GetKey(KeyCode.S))//press keyboard s to move down
{ this.transform.Translate(Vector3.back); }
if (Input.GetKey(KeyCode.A))//Press keyboard a to move to the left
{ this.transform.Translate(Vector3.left); } else if (Input.GetKey(KeyCode.D))//Press keyboard d to right shift { this.transform.Translate(Vector3.right); } }
}