ソースコードを貼付けてみる。
using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public bool facingRight = true; public float maxSpeed = 5; public float moveForce = 200; public float jumpForce = 200; public bool jump = false; public bool move = false; private Transform groundCheck; bool grounded = false; void Update () { grounded = Physics2D.Linecast(transform.position, transform.position - transform.up * 1.3f, 1 << LayerMask.NameToLayer("Ground")); if(Input.GetKeyDown("up") && grounded) jump = true; } void FixedUpdate () { float h = Input.GetAxis("Horizontal"); if(h * rigidbody2D.velocity.x < maxSpeed) rigidbody2D.AddForce(Vector2.right * h * moveForce); if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed) rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y); if(h > 0 && !facingRight) Flip(); else if(h < 0 && facingRight) Flip(); if (jump) { rigidbody2D.AddForce(new Vector2(0f, jumpForce)); jump = false; } } void Flip () { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } }
よさげ。
コピペもいっぱいした。