Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(PlayerPhisics))]
  5. public class PlayerControl : MonoBehaviour {
  6.  
  7.     // Player Handling
  8.     public float gravity = 20;
  9.     public float speed = 8;
  10.     public float acceleration = 30;
  11.     public float jumptHeight = 12;
  12.  
  13.     private float currentSpeed;
  14.     private float targetSpeed;
  15.     private Vector2 amountToMove;
  16.  
  17.     private PlayerPhisics playerPhisics;
  18.  
  19.     void Start () {
  20.         playerPhisics = GetComponent<PlayerPhisics>();
  21.     }
  22.    
  23.  
  24.     void Update () {
  25.  
  26.         if (playerPhisics.movementStopped)
  27.         {
  28.             targetSpeed = 0;
  29.             currentSpeed = 0;
  30.         }
  31.  
  32.         //Input
  33.         targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
  34.         currentSpeed = IncrementTowards(currentSpeed, targetSpeed, acceleration);
  35.  
  36.         if (playerPhisics.grounded) {
  37.             amountToMove.y = 0;
  38.             //Jump
  39.             if (Input.GetButtonDown("Jump")) {
  40.                 amountToMove.y = jumptHeight;
  41.             }
  42.         }
  43.  
  44.         amountToMove.x = currentSpeed;
  45.         amountToMove.y -= gravity * Time.deltaTime;
  46.         playerPhisics.Move(amountToMove * Time.deltaTime);
  47.     }
  48.  
  49.     //Increse it towards target by speed
  50.     private float IncrementTowards(float n, float target, float a) {
  51.         if (n == target) {
  52.             return n;
  53.         } else {
  54.             float dir = Mathf.Sign(target - n);
  55.             n += a * Time.deltaTime * dir;
  56.             return (dir == Mathf.Sign(target - n))? n: target;
  57.         }
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement