Skip to content

Instantly share code, notes, and snippets.

@Tachibana446
Created March 27, 2017 05:21
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 Tachibana446/1768898e4b5ebe49e50ef5190ef38e98 to your computer and use it in GitHub Desktop.
Save Tachibana446/1768898e4b5ebe49e50ef5190ef38e98 to your computer and use it in GitHub Desktop.
Unity Player Oparation
using UnityEngine;
public class Operatable : MonoBehaviour
{
public float speed = 5.0f;
private float jumpSpeed = 12.0f;
public float horizontal = 0, vertical = 0;
private Rigidbody2D rb2D;
private Transform tr;
/// <summary>
/// 現在右向きか
/// </summary>
private bool isRight = true;
// Use this for initialization
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
tr = GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
if (horizontal > 0.5)
{
rb2D.velocity = new Vector2(speed, rb2D.velocity.y);
tr.rotation = new Quaternion(tr.rotation.x, 180, tr.rotation.z, tr.rotation.w);
isRight = true;
}
else if (horizontal < -0.5)
{
rb2D.velocity = new Vector2(speed * -1, rb2D.velocity.y);
tr.rotation = new Quaternion(tr.rotation.x, 0, tr.rotation.z, tr.rotation.w);
isRight = false;
}
if (Input.GetButtonDown("Jump"))
{
rb2D.velocity = new Vector2(rb2D.velocity.x, jumpSpeed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment