Advertisement
Guest User

Unity3D Move

a guest
May 28th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace PlayerController
  5. {
  6.     [ RequireComponent( typeof( Rigidbody ) ) ]
  7.    
  8.     public class PlayerMove : MonoBehaviour
  9.     {
  10.         public      float walkAcceleration = 20f;
  11.         public      float walkDeacceleration = .1f;
  12.         public      float maxWalkSpeed = 5f;
  13.         public      float jumpForce = 150f;
  14.         public      float maxSlope = 75f;
  15.  
  16.         private     Vector2 horizontalMovement;
  17.         private     bool isMoving = false;
  18.         private     float walkDeaccelerationX = 0f;
  19.         private     float walkDeaccelerationZ = 0f;
  20.         private     float ref_walkDeaccelerationX = 0f;
  21.         private     float ref_walkDeaccelerationZ = 0f;
  22.         private     bool isGrounded = false;
  23.  
  24.         void Move()
  25.         {
  26.             // Move.
  27.             Vector3 forceDirection = new Vector3(
  28.                 Input.GetAxis( "Horizontal" ),
  29.                 0f,
  30.                 Input.GetAxis( "Vertical" )
  31.                 ) * walkAcceleration;
  32.            
  33.             if( forceDirection != Vector3.zero )
  34.             {
  35.                 if( isGrounded ) {
  36.                     rigidbody.AddRelativeForce( forceDirection );
  37.                 }
  38.                 else {
  39.                     rigidbody.AddRelativeForce( forceDirection * .1f );
  40.                 }
  41.  
  42.                 isMoving = true;
  43.             }
  44.             else
  45.             {
  46.                 if( isMoving )
  47.                 {
  48.                     if( isGrounded )
  49.                     {
  50.                         walkDeaccelerationX = Mathf.SmoothDamp(
  51.                             rigidbody.velocity.x,
  52.                             0f,
  53.                             ref ref_walkDeaccelerationX,
  54.                             walkDeacceleration );
  55.  
  56.                         walkDeaccelerationZ = Mathf.SmoothDamp(
  57.                             rigidbody.velocity.z,
  58.                             0f,
  59.                             ref ref_walkDeaccelerationZ,
  60.                             walkDeacceleration );
  61.  
  62.                         rigidbody.velocity = new Vector3(
  63.                             walkDeaccelerationX,
  64.                             rigidbody.velocity.y,
  65.                             walkDeaccelerationZ );
  66.  
  67.                         if( rigidbody.velocity.x == 0 && rigidbody.velocity.z == 0 ) {
  68.                             isMoving = false;
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             // Limit the velocity.
  75.             horizontalMovement = new Vector2( rigidbody.velocity.x, rigidbody.velocity.z );
  76.            
  77.             if( horizontalMovement.sqrMagnitude > maxWalkSpeed * maxWalkSpeed )
  78.             {
  79.                 horizontalMovement = horizontalMovement.normalized * maxWalkSpeed;
  80.                
  81.                 rigidbody.velocity = new Vector3(
  82.                     horizontalMovement.x,
  83.                     rigidbody.velocity.y,
  84.                     horizontalMovement.y );
  85.             }
  86.         }
  87.  
  88.         void Jump()
  89.         {
  90.             if( Input.GetKeyDown( KeyCode.Space ) && isGrounded )
  91.             {
  92.                 rigidbody.velocity = new Vector3(
  93.                     rigidbody.velocity.x,
  94.                     0,
  95.                     rigidbody.velocity.z );
  96.             }
  97.         }
  98.  
  99.         void Start() {
  100.             rigidbody.freezeRotation = true;
  101.         }
  102.        
  103.         void FixedUpdate()
  104.         {
  105.             Move();
  106.             Jump();
  107.         }
  108.  
  109.         void OnCollisionStay( Collision collisionInfo )
  110.         {
  111.             foreach( ContactPoint contactPoint in collisionInfo.contacts )
  112.             {
  113.                 if( Vector3.Angle( contactPoint.normal, Vector3.up ) <= maxSlope )
  114.                 {
  115.                     isGrounded = true;
  116.                 }
  117.             }
  118.         }
  119.  
  120.         void OnCollisionExit()
  121.         {
  122.             isGrounded = false;
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement