Advertisement
Miziziziz

Moving,Jumping,Climbing

Oct 29th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.    
  7.     public float speed = 5f;
  8.     public float jumpPower = 8f;
  9.    
  10.     bool grounded;
  11.    
  12.     public Transform groundCheck;
  13.     float groundedRadius = 0.2f;
  14.     public LayerMask groundMask;
  15.    
  16.     public bool inLadder = false;
  17.     float gravity;
  18.    
  19.     void Start()
  20.     {
  21.         gravity = rigidbody2D.gravityScale;
  22.     }
  23.  
  24.     void FixedUpdate()
  25.     {
  26.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundedRadius, groundMask);
  27.        
  28.         float x = Input.GetAxis ("Horizontal");
  29.         rigidbody2D.velocity = new Vector2 (x * speed, rigidbody2D.velocity.y);
  30.        
  31.         if(inLadder)
  32.         {
  33.             float y = Input.GetAxisRaw ("Vertical");
  34.             rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, y * speed);
  35.             rigidbody2D.gravityScale = 0;
  36.         }
  37.         else
  38.         {
  39.             rigidbody2D.gravityScale = gravity;
  40.             if(Input.GetButtonDown ("Jump") && grounded)
  41.             {
  42.                 rigidbody2D.AddForce(new Vector2(0f,jumpPower),ForceMode2D.Impulse);
  43.             }
  44.         }
  45.         inLadder = false;
  46.     }
  47.  
  48.     void OnTriggerStay2D(Collider2D coll)
  49.     {
  50.         if(coll.collider2D.tag == "Ladder")
  51.         {
  52.             inLadder = true;
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement