Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour {
- public float speed = 50f, maxspeed = 3, jumppow = 220f;
- public bool grounded = true, faceright = true, armright = true;
- public Rigidbody2D r2;
- public Animator anim;
- // Use this for initialization
- void Start () {
- r2 = gameObject.GetComponent<Rigidbody2D>();
- anim = gameObject.GetComponent<Animator>();
- }
- // Update is called once per frame
- void Update () {
- anim.SetBool("Grounded", grounded);
- anim.SetFloat("Speed", Mathf.Abs(r2.velocity.x));
- if (Input.GetKeyDown(KeyCode.Space))
- {
- if (grounded)
- {
- grounded = false;
- r2.AddForce(Vector2.up * jumppow);
- }
- }
- }
- private void FixedUpdate()
- {
- float h = Input.GetAxis("Horizontal");
- r2.AddForce((Vector2.right) * speed * h);
- if (r2.velocity.x > maxspeed)
- r2.velocity = new Vector2(maxspeed, r2.velocity.y);
- if (r2.velocity.x < -maxspeed)
- r2.velocity = new Vector2(-maxspeed, r2.velocity.y);
- if (armright && !faceright)
- flip();
- if (armright == false && faceright)
- flip();
- if (grounded)
- {
- r2.velocity = new Vector2(r2.velocity.x * 0.7f, r2.velocity.y);
- }
- }
- public void flip()
- {
- faceright = !faceright;
- Vector3 scale;
- scale = transform.localScale;
- scale.x *= -1;
- transform.localScale = scale;
- }
- }
Add Comment
Please, Sign In to add comment