using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { private Rigidbody2D rb2d; public float speed; public float maxSpeed=10; private SpriteRenderer sr; private Animator anim; void Start () { rb2d = gameObject.GetComponent (); sr = gameObject.GetComponent (); anim = gameObject.GetComponent (); } void Update () { anim.SetFloat ("speed", Mathf.Abs (Input.GetAxis ("Horizontal"))); Flip (); } void FixedUpdate(){ float h = Input.GetAxis ("Horizontal"); float v = Input.GetAxis ("Vertical"); rb2d.AddForce (Vector2.up * v * 10); rb2d.AddForce (Vector2.right * h*speed); if (rb2d.velocity.x > maxSpeed) { rb2d.velocity = new Vector2 (maxSpeed, rb2d.velocity.y); } if (rb2d.velocity.x < -maxSpeed) { rb2d.velocity = new Vector2 (-maxSpeed, rb2d.velocity.y); } if (Input.GetKeyDown (KeyCode.Space)) { rb2d.AddForce (Vector2.up * 100); } } void Flip(){ if (Input.GetAxis ("Horizontal") > 0) { sr.flipX = false; } else if (Input.GetAxis ("Horizontal") < 0){ sr.flipX = true; } } }