Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MovementController : MonoBehaviour
  6. {
  7.     public float velocity = 5;
  8.     public float turnSpeed = 10;
  9.     public float height = 0.5f;
  10.     public float heightPadding = 0.05f;
  11.     public LayerMask ground;
  12.     public float maxGroundAngle = 120;
  13.     public bool debug;
  14.  
  15.     Vector2 input;
  16.     float angle;
  17.     float groundAngle;
  18.  
  19.     Quaternion targetRotation;
  20.     Transform cam;
  21.  
  22.     Vector3 forward;
  23.     RaycastHit hitInfo;
  24.     bool grounded;
  25.  
  26.     void Start()
  27.     {
  28.         cam = Camera.main.transform;
  29.     }
  30.  
  31.     void Update()
  32.     {
  33.         GetInput();
  34.         CalculatedDirection();
  35.         CaluclateForward();
  36.         CaluclateGroundAngle();
  37.         CheckGround();
  38.         ApplyGravity();
  39.         DrawDebugLine();
  40.  
  41.         if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1) return;
  42.  
  43.         Rotate();
  44.         Move();
  45.     }
  46.  
  47.     void GetInput()
  48.     {
  49.         input.x = Input.GetAxisRaw("Horizontal");
  50.         input.y = Input.GetAxisRaw("Vertical");
  51.     }
  52.  
  53.     void CalculatedDirection()
  54.     {
  55.         angle = Mathf.Atan2(input.x, input.y);
  56.         angle = Mathf.Rad2Deg * angle;
  57.         angle += cam.eulerAngles.y;
  58.     }
  59.  
  60.     void Rotate()
  61.     {
  62.         targetRotation = Quaternion.Euler(0, angle, 0);
  63.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
  64.     }
  65.  
  66.     void Move()
  67.     {
  68.         if (groundAngle >= maxGroundAngle) return;
  69.         transform.position += forward * velocity * Time.deltaTime;
  70.     }
  71.  
  72.     void CaluclateForward()
  73.     {
  74.         if (!grounded)
  75.         {
  76.             forward = transform.forward;
  77.                 return;
  78.         }
  79.  
  80.         forward = Vector3.Cross(hitInfo.normal, -transform.right);
  81.     }
  82.  
  83.     void CaluclateGroundAngle()
  84.     {
  85.         if (!grounded)
  86.         {
  87.             groundAngle = 90;
  88.             return;
  89.         }
  90.  
  91.         groundAngle = Vector3.Angle(hitInfo.normal, transform.forward);
  92.     }
  93.  
  94.     void CheckGround()
  95.     {
  96.         if(Physics.Raycast(transform.position, -Vector3.up, out hitInfo, height + heightPadding, ground))
  97.         {
  98.             if(Vector3.Distance(transform.position, hitInfo.point) < height)
  99.             {
  100.                 transform.position = Vector3.Lerp(transform.position, transform.position + Vector3.up * height, 5 * Time.deltaTime);
  101.             }
  102.             grounded = true;
  103.         }
  104.         else
  105.         {
  106.             grounded = false;
  107.         }
  108.     }
  109.  
  110.     void ApplyGravity()
  111.     {
  112.         if (!grounded)
  113.         {
  114.             transform.position += Physics.gravity * Time.deltaTime;
  115.         }
  116.     }
  117.  
  118.     void DrawDebugLine()
  119.     {
  120.         if (!debug) return;
  121.  
  122.         Debug.DrawLine(transform.position, transform.position + forward * height * 2, Color.blue);
  123.         Debug.DrawLine(transform.position, transform.position - Vector3.up * height, Color.green);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement