how to make object move with keyboard in unity code example
Example 1: how to make an object move in unity
Vector3 input = new Vector3 (Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical");
Vector3 dir = input.normalized;
Vecotr3 vel = dir * speed * Time.deltaTime;
transform.Translate(vel);
Example 2: how to move object in unity with keyboar
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Vector3 position = this.transform.position;
position.x--;
this.transform.position = position;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Vector3 position = this.transform.position;
position.x++;
this.transform.position = position;
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Vector3 position = this.transform.position;
position.y++;
this.transform.position = position;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Vector3 position = this.transform.position;
position.y--;
this.transform.position = position;
}