Created
September 22, 2017 23:10
-
-
Save naojitaniguchi/69de3d0993230b05b917fd0fe0acc69c to your computer and use it in GitHub Desktop.
2D Character control sample for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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