Skip to content

Instantly share code, notes, and snippets.

@dorako321
Created May 3, 2019 08:38
Show Gist options
  • Save dorako321/b37f929c54d4138f9b2c83226183df20 to your computer and use it in GitHub Desktop.
Save dorako321/b37f929c54d4138f9b2c83226183df20 to your computer and use it in GitHub Desktop.

player.cs

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{
  // 移動スピード
  public float speed = 5;

  void Update ()
  {
    // 右・左
    float x = Input.GetAxisRaw ("Horizontal");

    // 上・下
    float y = Input.GetAxisRaw ("Vertical");

    // 移動する向きを求める
    Vector2 direction = new Vector2 (x, y).normalized;

    // 移動する向きとスピードを代入する
    GetComponent<Rigidbody2D>().velocity = direction * speed;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment