Advertisement
vjanomolee

Character Movement WIP

Feb 20th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class CharacterMovement : MonoBehaviour {
  5.  
  6.  
  7.     public float maxSpeed = 6.0f;
  8.     public bool facingRight = true;
  9.     public float moveDirection;
  10.     public Rigidbody rb;
  11.     public float jumpSpeed = 100.0f;
  12.  
  13.     public bool grounded = false;
  14.     public Transform groundCheck;
  15.     public float groundRadius = 0.2f;
  16.     public LayerMask whatIsGround;
  17.  
  18.     void Awake()
  19.     {
  20.         rb = GetComponent<Rigidbody> ();
  21.         groundCheck = GameObject.Find ("GroundCheck").transform;
  22.     }
  23.  
  24.     // Use this for physics stuff (like rigid bodies etc)
  25.     void FixedUpdate ()
  26.     {
  27.         groundded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
  28.  
  29.         rb.velocity = new Vector2(moveDirection * maxSpeed, rb.velocity.y);
  30.         if (moveDirection > 0.0f && !facingRight) {
  31.             Flip ();
  32.         }
  33.         else if (moveDirection < 0.0f && facingRight) {
  34.             Flip ();
  35.         }
  36.     }
  37.    
  38.     // Update is called once per frame
  39.     void Update () {
  40.         moveDirection = Input.GetAxis ("Horizontal");
  41.  
  42.         if(grounded && Input.GetKeyDown("Space")
  43.             {
  44.                 rb.AddForce(new Vector2(0, jumpSpeed));
  45.             }
  46.     }
  47.  
  48.     void Flip()
  49.     {
  50.         facingRight = !facingRight;
  51.         transform.Rotate (Vector3.up, 180.0f, Space.World);
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement