Skip to content

Instantly share code, notes, and snippets.

@naojitaniguchi
Created September 22, 2017 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naojitaniguchi/69de3d0993230b05b917fd0fe0acc69c to your computer and use it in GitHub Desktop.
Save naojitaniguchi/69de3d0993230b05b917fd0fe0acc69c to your computer and use it in GitHub Desktop.
2D Character control sample for Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KeyInput : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.UpArrow))
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y+ speed * Time.deltaTime, gameObject.transform.position.z);
}
if (Input.GetKey(KeyCode.DownArrow))
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - speed * Time.deltaTime, gameObject.transform.position.z);
}
if (Input.GetKey(KeyCode.RightArrow))
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x + speed * Time.deltaTime, gameObject.transform.position.y , gameObject.transform.position.z);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x - speed * Time.deltaTime, gameObject.transform.position.y, gameObject.transform.position.z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment