Advertisement
dronkowitz

UltimatePlayerMovement.cs

Sep 1st, 2021 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class UltimatePlayerMovement : MonoBehaviour
  6. {
  7.     #region constant variables
  8.     public const float acceleration = 5f;
  9.     public const float deceleration = 3f;
  10.     public const float runSpeed = 20f;
  11.     public const float airSpeed = 15f;
  12.     #endregion
  13.     public float currentSpeed = runSpeed;
  14.     #region public variables
  15.     public GameObject leftFollower;
  16.     public GameObject rightFollower;
  17.     public GameObject sonic;
  18.     public GameObject superSonic;
  19.     public Rigidbody body;
  20.     public Transform cam;
  21.     public LayerMask groundMask;
  22.     public Vector3 moveForce;
  23.     public bool tutorialPlaying = false;
  24.     #endregion
  25.  
  26.     #region private variables
  27.     public GameObject currentCharacter;
  28.     public bool useGravity;
  29.     private Quaternion CameraWorldFoward
  30.     {
  31.         get
  32.         {
  33.             return Quaternion.LookRotation(cam.forward, transform.up);
  34.         }
  35.     }
  36.  
  37.     public bool isGrounded;
  38.     private float jumpSustainTime = 0;
  39.     private bool IsSurrendered = false;
  40.  
  41.     private float CurrentMaxSpeed
  42.     {
  43.         get
  44.         {
  45.             return isGrounded ? currentSpeed : airSpeed;
  46.         }
  47.     }
  48.  
  49.     public static bool Controllable { get; internal set; }
  50.     public bool TrickZone { get; internal set; }
  51.     public object LeftTeamMember { get; internal set; }
  52.     public object RightTeamMember { get; internal set; }
  53.     public object TeamSetup { get; private set; }
  54.     #endregion
  55.  
  56.  
  57.     private void Awake()
  58.     {
  59.         cam = FindObjectOfType<CameraController>().transform;
  60.     }
  61.     private void Update()
  62.     {
  63.         isGrounded = Physics.CheckSphere(transform.position + transform.up * 0.1f, 0.49f, groundMask);
  64.         if (tutorialPlaying) return;
  65.  
  66.         if (anim != null)
  67.         {
  68.             anim.SetBool("InAir", !isGrounded);
  69.         }
  70.         if (isGrounded == true)
  71.         {
  72.             leftFollower.GetComponent<FollowerNavigation>().agent.enabled = true;
  73.             rightFollower.GetComponent<FollowerNavigation>().agent.enabled = true;
  74.         }
  75.         //RotateToGround();
  76.     }
  77.  
  78.     private void FixedUpdate()
  79.     {
  80.         Move();
  81.     }
  82.     private Animator anim;
  83.     public void SetupAnimation()
  84.     {
  85.         anim = GetComponentInChildren<Animator>();
  86.     }
  87.  
  88.  
  89.     public void OldMove()
  90.     {
  91.         Vector3 right = Vector3.Cross(transform.up, cam.forward);
  92.         Vector3 forward = Vector3.Cross(right, transform.up);
  93.  
  94.         if (tutorialPlaying)
  95.         {
  96.             right = Vector3.zero; forward = Vector3.zero;
  97.         }
  98.  
  99.         Vector3 mov = Vector3.zero;
  100.  
  101.  
  102.  
  103.         if (!IsSurrendered)
  104.         {
  105.             mov = Input.GetAxis("Vertical") * forward + Input.GetAxis("Horizontal") * right;
  106.             Jump();
  107.             Turn();
  108.         }
  109.  
  110.  
  111.         Vector3 velocityXZ = body.velocity;
  112.         velocityXZ.y = 0;
  113.         direction = mov * currentSpeed;
  114.         if (isGrounded)
  115.         {
  116.             body.useGravity = false;
  117.             //body.velocity = mov * runSpeed;
  118.             body.MovePosition(transform.position + (mov * currentSpeed * Time.fixedDeltaTime));
  119.         }
  120.         else
  121.         {
  122.             body.useGravity = true;
  123.             //body.AddForce(Vector3.down * 90);
  124.             body.AddForce(mov * 50);
  125.  
  126.             if (velocityXZ.magnitude > currentSpeed)
  127.             {
  128.                 velocityXZ = velocityXZ.normalized * currentSpeed;
  129.                 velocityXZ.y = body.velocity.y;
  130.                 body.velocity = velocityXZ;
  131.             }
  132.         }
  133.         if (anim != null)
  134.         {
  135.             anim.SetFloat("Speed", velocityXZ.magnitude);
  136.         }
  137.  
  138.     }
  139.  
  140.     public void Move()
  141.     {
  142.         Vector3 right = Vector3.Cross(transform.up, cam.forward);
  143.         Vector3 forward = Vector3.Cross(right, transform.up);
  144.  
  145.  
  146.         if (tutorialPlaying)
  147.         {
  148.             right = Vector3.zero; forward = Vector3.zero;
  149.         }
  150.  
  151.         Vector3 mov = Vector3.zero;
  152.         if (!IsSurrendered)
  153.         {
  154.             mov = Input.GetAxis("Vertical") * forward + Input.GetAxis("Horizontal") * right;
  155.             Jump();
  156.             Turn();
  157.         }
  158.         if (isGrounded)
  159.         {
  160.             GroundMovement(mov);
  161.         }
  162.         else
  163.         {
  164.             AirMovement(mov);
  165.         }
  166.  
  167.     }
  168.  
  169.     private void GroundMovement(Vector3 mov)
  170.     {
  171.         maxAirSpeed = currentSpeed;
  172.         direction = mov * currentSpeed;
  173.         if (direction.magnitude > currentSpeed)
  174.         {
  175.             direction = direction.normalized * currentSpeed;
  176.         }
  177.  
  178.         body.MovePosition(transform.position + transform.TransformDirection(direction) * Time.fixedDeltaTime);
  179.     }
  180.     private float maxAirSpeed;
  181.     private float airControl = 20f;
  182.     private void AirMovement(Vector3 mov)
  183.     {
  184.         body.AddForce(transform.TransformDirection(mov * airControl));
  185.         Vector3 veloXZ = body.velocity;
  186.         veloXZ.y = 0;
  187.         if (veloXZ.magnitude > maxAirSpeed)
  188.         {
  189.             veloXZ = veloXZ.normalized * maxAirSpeed;
  190.             veloXZ.y = body.velocity.y;
  191.             body.velocity = veloXZ;
  192.         }
  193.     }
  194.     private float jumpHeight = 6f;
  195.     private Vector3 direction;
  196.  
  197.     public void Jump()
  198.     {
  199.         if (Input.GetKey(KeyCode.Space) && isGrounded)
  200.         {
  201.             Vector3 velo = transform.TransformDirection(direction * 0.8f);
  202.             velo.y = Mathf.Sqrt(jumpHeight * -2 * Physics.gravity.y);
  203.             body.velocity = velo;
  204.         }
  205.  
  206.  
  207.     }
  208.     public void Turn()
  209.     {
  210.         transform.Rotate(transform.up, Input.GetAxis("Mouse X"));
  211.     }
  212.     public void RotateToGround()
  213.     {
  214.         if (!isGrounded)
  215.         {
  216.             Vector3 cross = Vector3.Cross(transform.right, Vector3.up);
  217.             Quaternion newrot = Quaternion.LookRotation(cross);
  218.             transform.rotation = Quaternion.LerpUnclamped(transform.rotation, newrot, Time.deltaTime * 100);
  219.             return;
  220.         }
  221.  
  222.         RaycastHit hit;
  223.         Vector3 origin = transform.position + transform.up * 0.5f;
  224.         if (Physics.SphereCast(origin, 0.49f, -transform.up, out hit, 0.6f, groundMask))//initial raycast to see if the ground is close enough to snap to
  225.         {
  226.            
  227.  
  228.             Vector3 newup = hit.normal;//angle of the initial hit
  229.             float angle = Vector3.Angle(transform.up, newup);
  230.  
  231.             if (angle > 30)
  232.             {
  233.                
  234.             }
  235.  
  236.            
  237.  
  238.             Vector3 cross = Vector3.Cross(transform.right, newup);//new foward direction
  239.  
  240.  
  241.             Quaternion newrot = Quaternion.LookRotation(cross);
  242.  
  243.             transform.rotation = Quaternion.LerpUnclamped(transform.rotation, newrot, Time.deltaTime * 100f);
  244.  
  245.             //transform.rotation = Quaternion.FromToRotation(transform.up, angle);
  246.  
  247.             //transform.position = hit.point + transform.up * 0.01f;
  248.  
  249.  
  250.         }
  251.        
  252.     }
  253.  
  254.     public void SurrenderControl(Vector2 up, float newSurrenderTime)
  255.     {
  256.         StopCoroutine(Surrender(0));
  257.         StartCoroutine(Surrender(newSurrenderTime));
  258.     }
  259.  
  260.     private IEnumerator Surrender(float time)
  261.     {
  262.         IsSurrendered = true;
  263.         yield return new WaitForSeconds(time);
  264.         IsSurrendered = false;
  265.     }
  266.  
  267.     public void Launch(Vector3 direction, float height)
  268.     {
  269.         GetComponent<Rigidbody>().velocity = Mathf.Sqrt(height * -2 * Physics.gravity.y) * direction;
  270.  
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement