Advertisement
Teonnyn

2D Platformer Motor

Feb 17th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player_Motor : MonoBehaviour {
  5.  
  6.     public static Player_Motor Instance;
  7.    
  8.  
  9.     public float verticalVelocity { get; set; }
  10.  
  11.     public float horizontalVelocity { get; set; }
  12.  
  13.     public float verticalForce = 6f;
  14.  
  15.     public float horizontalForce = 6f;
  16.  
  17.     public float drag = 2.0f;
  18.  
  19.     public Vector3 moveVector { get; set; }
  20.  
  21.     public Transform _Cam;
  22.  
  23.     float gravity = 21.8f;
  24.  
  25.     float terminalVelocity = 20.0f;
  26.  
  27.     void Awake() {
  28.         Instance = this;
  29.     }
  30.  
  31.     public void UpdateMotor () {
  32.         ProcessMotion ();
  33.  
  34.         Debug.Log (moveVector);
  35.  
  36.         _Cam.transform.position = new Vector3 (transform.position.x, transform.position.y, _Cam.transform.position.z);
  37.     }
  38.  
  39.     void ProcessMotion() {
  40.         moveVector = transform.TransformDirection (moveVector);
  41.  
  42.         if (moveVector.magnitude > 1) {
  43.             moveVector = Vector3.Normalize(moveVector);
  44.         }
  45.  
  46.         horizontalVelocity = horizontalForce;
  47.  
  48.         moveVector = new Vector3 (horizontalVelocity, verticalVelocity, 0);
  49.    
  50.         applyDrag();
  51.  
  52.         applyGravity();
  53.  
  54.         Player_Controller.characterController.Move (moveVector * Time.deltaTime);
  55.     }
  56.  
  57.     void applyDrag() {
  58.  
  59.         if (moveVector.x > 0) {
  60.             moveVector = new Vector3(horizontalVelocity - drag * Time.deltaTime, moveVector.y, 0);
  61.         }
  62.     }
  63.  
  64.     void applyGravity() {
  65.         if (moveVector.y > -terminalVelocity) {
  66.             moveVector = new Vector3(moveVector.x, moveVector.y - gravity * Time.deltaTime, 0);
  67.         }
  68.  
  69.         if (Player_Controller.characterController.isGrounded && moveVector.y < -1) {
  70.             moveVector = new Vector3 (moveVector.x, -1, 0);
  71.         }
  72.     }
  73.  
  74.     public void Leap() {
  75.         //if (Player_Controller.characterController.isGrounded ) {
  76.         verticalVelocity = verticalForce;
  77.         //}
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement