Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- public class CharacterMovement : MonoBehaviour {
- public float maxSpeed = 6.0f;
- public bool facingRight = true;
- public float moveDirection;
- public Rigidbody rb;
- public float jumpSpeed = 100.0f;
- public bool grounded = false;
- public Transform groundCheck;
- public float groundRadius = 0.2f;
- public LayerMask whatIsGround;
- void Awake()
- {
- rb = GetComponent<Rigidbody> ();
- groundCheck = GameObject.Find ("GroundCheck").transform;
- }
- // Use this for physics stuff (like rigid bodies etc)
- void FixedUpdate ()
- {
- groundded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
- rb.velocity = new Vector2(moveDirection * maxSpeed, rb.velocity.y);
- if (moveDirection > 0.0f && !facingRight) {
- Flip ();
- }
- else if (moveDirection < 0.0f && facingRight) {
- Flip ();
- }
- }
- // Update is called once per frame
- void Update () {
- moveDirection = Input.GetAxis ("Horizontal");
- if(grounded && Input.GetKeyDown("Space")
- {
- rb.AddForce(new Vector2(0, jumpSpeed));
- }
- }
- void Flip()
- {
- facingRight = !facingRight;
- transform.Rotate (Vector3.up, 180.0f, Space.World);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement