Advertisement
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_Move : MonoBehaviour {
- public int playerSpeed = 10;
- public bool facingRight = true;
- public int playerJumpPower = 1250;
- public float moveX;
- public bool isGrounded;
- // Use this for initialization
- void Start() {
- }
- // Update is called once per frame
- void Update() {
- PlayerMove();
- }
- void PlayerMove() {
- //CONTROLS
- moveX = Input.GetAxis("Horizontal");
- if (Input.GetButtonDown("Jump") && isGrounded == true){
- Jump();
- }
- //ANIMATIONS
- //PLAYER DIRECTIONS
- if (moveX < 0.0f && facingRight == false) {
- FlipPlayer();
- } else if (moveX > 0.0f && facingRight == true) {
- FlipPlayer();
- }
- //PHYSICS
- gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
- }
- void Jump() {
- //JUMPING CODE
- GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);
- isGrounded = false;
- }
- void FlipPlayer() {
- facingRight = !facingRight;
- Vector2 localScale = gameObject.transform.localScale;
- localScale.x *= -1;
- transform.localScale = localScale;
- }
- void onCollisionEnter2D (Collision2D col){
- Debug.Log ("Player has collided with " + col.collider.name);
- if (col.gameObject.tag == "ground") {
- isGrounded = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement