Guest User

Untitled

a guest
Dec 12th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(Rigidbody))]
  5. public class Character:MonoBehaviour {
  6.    
  7.     public float footOffset = 0.1f;
  8.     public float footAnchorRatio = 0.5f;
  9.     public float stepSmoothing = 50.0f;
  10.    
  11.     public float groundedDrag = 1.0f;
  12.     public float airLateralDrag = 0.1f;
  13.     public bool isGrounded = false;
  14.     public Vector3 groundPos;
  15.     public Vector3 groundNormal;
  16.     public Vector3 footNormal;
  17.    
  18.     void Start() {
  19.        
  20.     }
  21.    
  22.     void FixedUpdate() {
  23.         CapsuleCollider capsuleCollider = gameObject.GetComponent<CapsuleCollider>();
  24.        
  25.         RaycastHit[] hits;
  26.        
  27.         float footRadius = capsuleCollider.radius*0.75f;
  28.         Vector3 cPoint1 = transform.position+new Vector3(0,0,0);
  29.         float shpherecastDistance = ((capsuleCollider.height*0.5f)-footRadius)+footOffset;
  30.         hits = Physics.SphereCastAll(cPoint1,footRadius,new Vector3(0,-1,0),shpherecastDistance);
  31.         Vector3 shperecastEndPoint = cPoint1+(new Vector3(0,-1,0)*shpherecastDistance);
  32.         Debug.DrawLine(cPoint1,shperecastEndPoint,new Color(1,1,0,1));
  33.         Debug.DrawLine(cPoint1+(new Vector3(0,-1,0)*shpherecastDistance),shperecastEndPoint+new Vector3(footRadius,0,0),new Color(1,0,0,1));
  34.         Debug.DrawLine(cPoint1+(new Vector3(0,-1,0)*shpherecastDistance),shperecastEndPoint+new Vector3(0,-footRadius,0),new Color(0,1,0,1));
  35.        
  36.         isGrounded = false;
  37.        
  38.         Vector3 characterPos = transform.position;
  39.         if (hits.Length > 0) {
  40.             RaycastHit bestRaycastHit = new RaycastHit();
  41.             RaycastHit bestSpherecastHit = new RaycastHit();
  42.            
  43.             //Ray bestRay = new Ray(Vector3.zero,Vector3.zero);
  44.            
  45.             for (int i = 0; i < hits.Length; i++) {
  46.                 //float hitDif = characterPos.y-hits[i].point.y;
  47.                 RaycastHit hit;
  48.                 Vector3 rayOrigin = new Vector3(hits[i].point.x,characterPos.y-capsuleCollider.height,hits[i].point.z);
  49.                 Ray ray = new Ray(rayOrigin,new Vector3(0,1,0));
  50.                
  51.                 if (capsuleCollider.Raycast(ray,out hit,capsuleCollider.height*2.0f)) {
  52.                     float hitDif = hit.point.y-hits[i].point.y;
  53.                     float bestHitDif = bestRaycastHit.point.y-bestSpherecastHit.point.y;
  54.                    
  55.                     if (i == 0 || hitDif < bestHitDif) {
  56.                         bestRaycastHit = hit;
  57.                         bestSpherecastHit = hits[i];
  58.                         //bestRay = ray;
  59.                     }
  60.                    
  61.                 }
  62.             }
  63.            
  64.             isGrounded = true;
  65.             groundPos = bestSpherecastHit.point;
  66.             groundNormal = bestSpherecastHit.normal;
  67.             footNormal = bestRaycastHit.normal;
  68.            
  69.            
  70.             if (rigidbody.velocity.y <= 0.0f) {
  71.                 float charHitDif = characterPos.y-bestRaycastHit.point.y;
  72.                 Vector3 newPos = characterPos;
  73.                 newPos.y = bestSpherecastHit.point.y;
  74.                 newPos.y += charHitDif+(footOffset*footAnchorRatio);
  75.                 newPos.y = Mathf.Lerp(characterPos.y,newPos.y,Time.deltaTime*stepSmoothing);
  76.                 transform.position = newPos;
  77.             }
  78.            
  79.             Debug.DrawLine(bestSpherecastHit.point,bestRaycastHit.point,new Color(0,0,1,1));
  80.            
  81.         }
  82.        
  83.         if (isGrounded) {
  84.             Vector3 vel = rigidbody.velocity;
  85.             vel.y = Mathf.Max(vel.y,0.0f);
  86.             vel.x /= 1.0f+(groundedDrag*Time.deltaTime);
  87.             vel.z /= 1.0f+(groundedDrag*Time.deltaTime);
  88.             rigidbody.velocity = vel;
  89.         }
  90.         else {
  91.             Vector3 vel = rigidbody.velocity;
  92.             vel.x /= 1.0f+(airLateralDrag*Time.deltaTime);
  93.             vel.z /= 1.0f+(airLateralDrag*Time.deltaTime);
  94.             rigidbody.velocity = vel;
  95.         }
  96.        
  97.     }
  98. }
Add Comment
Please, Sign In to add comment