Advertisement
Guest User

CharacterMovement.cs

a guest
Sep 26th, 2018
8,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CharacterMovement: MonoBehaviour {
  6.  
  7.     public float floorOffsetY;
  8.     public float moveSpeed = 6f;
  9.     public float rotateSpeed = 10f;
  10.  
  11.     public string animationFloatname;
  12.  
  13.     Rigidbody rb;
  14.     Animator anim;
  15.     float vertical;
  16.     float horizontal;
  17.     Vector3 moveDirection;
  18.     float inputAmount;
  19.     Vector3 raycastFloorPos;
  20.     Vector3 floorMovement;
  21.     Vector3 gravity;
  22.     Vector3 CombinedRaycast;
  23.  
  24.     // Use this for initialization
  25.     void Start () {
  26.         rb = GetComponent<Rigidbody>();
  27.         anim = GetComponent<Animator>();
  28.     }
  29.  
  30.     private void Update()
  31.     {
  32.         // reset movement
  33.         moveDirection = Vector3.zero;
  34.         // get vertical and horizontal movement input (controller and WASD/ Arrow Keys)
  35.         vertical = Input.GetAxis("Vertical");
  36.         horizontal = Input.GetAxis("Horizontal");
  37.  
  38.         // base movement on camera
  39.         Vector3 correctedVertical = vertical * Camera.main.transform.forward;
  40.         Vector3 correctedHorizontal = horizontal * Camera.main.transform.right;
  41.  
  42.         Vector3 combinedInput = correctedHorizontal + correctedVertical;
  43.         // normalize so diagonal movement isnt twice as fast, clear the Y so your character doesnt try to
  44.         // walk into the floor/ sky when your camera isn't level
  45.          moveDirection = new Vector3((combinedInput).normalized.x, 0, (combinedInput).normalized.z);
  46.        
  47.         // make sure the input doesnt go negative or above 1;
  48.         float inputMagnitude = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
  49.         inputAmount = Mathf.Clamp01(inputMagnitude);
  50.  
  51.       // rotate player to movement direction
  52.         Quaternion rot = Quaternion.LookRotation(moveDirection);
  53.         Quaternion targetRotation = Quaternion.Slerp(transform.rotation, rot, Time.fixedDeltaTime * inputAmount * rotateSpeed);
  54.         transform.rotation = targetRotation;
  55.  
  56.         // handle animation blendtree for walking
  57.         anim.SetFloat(animationFloatname, inputAmount, 0.2f, Time.deltaTime);
  58.     }
  59.  
  60.    
  61.    private void FixedUpdate () {
  62.         // if not grounded , increase down force
  63.         if(FloorRaycasts(0,0,0.6f) == Vector3.zero)
  64.         {
  65.             gravity += Vector3.up * Physics.gravity.y * Time.fixedDeltaTime;
  66.         }
  67.        
  68.         // actual movement of the rigidbody + extra down force
  69.         rb.velocity = (moveDirection * moveSpeed * inputAmount) + gravity;
  70.  
  71.         // find the Y position via raycasts
  72.         floorMovement =  new Vector3(rb.position.x, FindFloor().y + floorOffsetY, rb.position.z);
  73.  
  74.         // only stick to floor when grounded
  75.         if(FloorRaycasts(0,0, 0.6f) != Vector3.zero && floorMovement != rb.position)
  76.         {
  77.             // move the rigidbody to the floor
  78.             rb.MovePosition(floorMovement);
  79.             gravity.y = 0;
  80.         }
  81.        
  82.     }
  83.  
  84.     Vector3 FindFloor()
  85.     {
  86.         // width of raycasts around the centre of your character
  87.         float raycastWidth = 0.25f;
  88.         // check floor on 5 raycasts   , get the average when not Vector3.zero  
  89.          int floorAverage = 1;
  90.  
  91.         CombinedRaycast = FloorRaycasts(0, 0, 1.6f);
  92.         floorAverage += (getFloorAverage(raycastWidth, 0) + getFloorAverage(-raycastWidth, 0) + getFloorAverage(0, raycastWidth) + getFloorAverage(0, -raycastWidth));
  93.      
  94.         return CombinedRaycast / floorAverage;
  95.     }
  96.  
  97.     // only add to average floor position if its not Vector3.zero
  98.     int getFloorAverage(float offsetx, float offsetz)
  99.     {
  100.        
  101.         if (FloorRaycasts(offsetx, offsetz, 1.6f) != Vector3.zero)
  102.         {
  103.             CombinedRaycast += FloorRaycasts(offsetx, offsetz, 1.6f);
  104.             return 1;
  105.         }
  106.         else { return 0; }
  107.     }
  108.  
  109.  
  110.     Vector3 FloorRaycasts(float offsetx, float offsetz, float raycastLength)
  111.     {
  112.         RaycastHit hit;
  113.         // move raycast
  114.         raycastFloorPos = transform.TransformPoint(0 + offsetx, 0 + 0.5f, 0 + offsetz);
  115.  
  116.         Debug.DrawRay(raycastFloorPos, Vector3.down, Color.magenta);
  117.         if (Physics.Raycast(raycastFloorPos, -Vector3.up, out hit, raycastLength))
  118.         {
  119.             return hit.point;
  120.         }
  121.         else return Vector3.zero;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement