Advertisement
dronkowitz

NewPlayerMovement.cs

May 27th, 2021 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.94 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NewPlayerMovement : MonoBehaviour
  6. {
  7.     public CharacterController controller;
  8.     public GameObject teamSetup;
  9.     public enum STATE
  10.     {
  11.         NORMAL,
  12.         HURT,
  13.         DASHING,
  14.         SPECIAL,
  15.         ATTACK,
  16.         DEAD,
  17.  
  18.     }
  19.     public bool isSuper = false;
  20.     private const float maxSpeed = 8;
  21.     private const float maxSuperSpeed = 12;
  22.  
  23.     public float CurrentMaxSpeed
  24.     {
  25.         get
  26.         {
  27.             if (isSuper) return maxSuperSpeed;
  28.             return maxSpeed;
  29.         }
  30.     }
  31.  
  32.     private const float acceleration = 5;
  33.     private const float superAcceleration = 7;
  34.     public float CurrentAcceleration
  35.     {
  36.         get
  37.         {
  38.             if (isSuper) return superAcceleration;
  39.             return acceleration;
  40.         }
  41.     }
  42.  
  43.     private const float jumpHeight = 3;
  44.     public float currentJumpHeight = 3;
  45.  
  46.     private Vector3 gravity;
  47.  
  48.     private Vector3 velocity = Vector3.zero;
  49.     private Vector3 previousPostition;
  50.     private Vector2 movement = Vector2.zero;
  51.     public LayerMask groundMask;
  52.     private bool isJumping;
  53.     public bool isGrounded;
  54.     public bool inAir
  55.     {
  56.         get
  57.         {
  58.             return (!isGrounded || velocity.y > 0);
  59.         }
  60.     }
  61.     private bool jump;
  62.     public float currentSpeed;
  63.     public float maxRunSpeed;
  64.     public float maxWalkSpeed;
  65.     public GameObject Leftpos;
  66.     public GameObject Rightpos;
  67.     public GameObject LeftTeamMember;
  68.     public GameObject RightTeamMember;
  69.     public Rigidbody body;
  70.     public Camera MainCamera;
  71.  
  72.  
  73.     public float ToZero(float initialNumber, float deltavalue)
  74.     {
  75.         if (initialNumber > 0)
  76.         {
  77.             initialNumber -= deltavalue;
  78.             initialNumber = Mathf.Clamp(initialNumber, 0, Mathf.Infinity);
  79.         }
  80.         if (initialNumber < 0)
  81.         {
  82.             initialNumber += deltavalue;
  83.             initialNumber = Mathf.Clamp(initialNumber, Mathf.NegativeInfinity, 0);
  84.         }
  85.         return initialNumber;
  86.     }
  87.  
  88.     // Start is called before the first frame update
  89.     void Start()
  90.     {
  91.         MainCamera = Camera.main;
  92.         previousPostition = transform.position;
  93.         gravity = Physics.gravity;
  94.     }
  95.  
  96.     // Update is called once per frame
  97.     void Update()
  98.     {
  99.         if (controllable == true)
  100.         {
  101.  
  102.  
  103.             if (surrendered) //If surrended count down timer and restore control when set to 0
  104.             {
  105.                 surrenderDuration -= Time.deltaTime;
  106.                 if (surrenderDuration <= 0) surrendered = false;
  107.             }
  108.             else Inputs();
  109.             Movement();
  110.  
  111.         }
  112.         if (Input.GetKeyDown(KeyCode.Home))
  113.         {
  114.             BecomeSuper();
  115.         }
  116.     }
  117.     public void BecomeSuper()
  118.     {
  119.         GameInstance.greenEmerald = true;
  120.         GameInstance.blueEmerald = true;
  121.         GameInstance.yellowEmerald = true;
  122.         GameInstance.whiteEmerald = true;
  123.         GameInstance.lightBlueEmerald = true;
  124.         GameInstance.purpleEmerald = true;
  125.         GameInstance.redEmerald = true;
  126.  
  127.         teamSetup.GetComponent<TeamSetup>().SwapForSuper();
  128.     }
  129.  
  130.     private void LateUpdate()
  131.     {
  132.         transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Vector3.up);
  133.  
  134.         RaycastHit groundHit;
  135.  
  136.         if (Physics.Raycast(transform.position + (transform.up * 0.1f), -transform.up, out groundHit, 0.3f, groundMask) && velocity.y < 0)
  137.         {
  138.             Vector3 velocityXZ = velocity;
  139.             velocityXZ.y = 0;
  140.             if (velocityXZ.sqrMagnitude > 0.3f)
  141.                 transform.localRotation = Quaternion.LookRotation(velocity, transform.up);
  142.  
  143.  
  144.             velocity.y = -0.5f;
  145.  
  146.  
  147.             Vector3 hitAngle = groundHit.normal;
  148.             transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(Vector3.Cross(hitAngle, -transform.right)), 360 * Time.deltaTime);
  149.             transform.position = groundHit.point + (transform.up * 0.08f);
  150.  
  151.         }
  152.         else
  153.         {
  154.             transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Vector3.up);
  155.  
  156.         }
  157.     }
  158.  
  159.     public void Inputs()
  160.     {
  161.         Vector2 mov = Vector2.zero;
  162.         mov.x = Input.GetAxis("Horizontal");
  163.         mov.y = Input.GetAxis("Vertical");
  164.         if (mov.x == 0)
  165.         {
  166.             movement.x = ToZero(movement.x, Time.deltaTime * acceleration);
  167.         }
  168.         else
  169.         {
  170.             if ((mov.x > 0 && movement.x > 0) || (mov.x < 0 && movement.x < 0))
  171.             {
  172.                 movement.x += mov.x * acceleration * Time.deltaTime;
  173.             }
  174.             else
  175.             {
  176.                 movement.x += mov.x * acceleration * Time.deltaTime * 3;
  177.             }
  178.  
  179.         }
  180.         if (mov.y == 0)
  181.         {
  182.             movement.y = ToZero(movement.y, Time.deltaTime * acceleration);
  183.         }
  184.         else
  185.         {
  186.             if ((mov.y > 0 && movement.y > 0) || (mov.y < 0 && movement.y < 0))
  187.             {
  188.                 movement.y += mov.y * acceleration * Time.deltaTime;
  189.             }
  190.             else
  191.             {
  192.                 movement.y += mov.y * acceleration * Time.deltaTime * 3;
  193.             }
  194.  
  195.         }
  196.  
  197.  
  198.         movement.x = Mathf.Clamp(movement.x, -CurrentMaxSpeed, CurrentMaxSpeed);
  199.         movement.y = Mathf.Clamp(movement.y, -CurrentMaxSpeed, CurrentMaxSpeed);
  200.  
  201.         float rot = Input.GetAxis("Mouse X");
  202.         transform.Rotate(transform.up * rot);
  203.  
  204.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded) isJumping = true;
  205.     }
  206.    
  207.     public void Movement()
  208.     {
  209.         isGrounded = Physics.CheckSphere(transform.position + transform.up * 0.45f, 0.6f, groundMask);
  210.  
  211.  
  212.         Transform cam = Camera.main.transform;
  213.         Vector3 mov = cam.forward * movement.y + cam.right * movement.x;
  214.         if (isJumping)
  215.         {
  216.             isJumping = false;
  217.  
  218.  
  219.  
  220.             mov += Mathf.Sqrt(currentJumpHeight * -2 * gravity.y) * transform.up;
  221.             velocity = mov;
  222.  
  223.  
  224.             LeftTeamMember.GetComponent<FollowerNavigation>().Jump(velocity);
  225.             RightTeamMember.GetComponent<FollowerNavigation>().Jump(velocity);
  226.  
  227.  
  228.         }
  229.         if (!isGrounded || velocity.y > 0)
  230.         {
  231.             mov = velocity + mov * Time.deltaTime;
  232.             velocity = mov;
  233.  
  234.  
  235.         }
  236.         velocity += gravity * Time.deltaTime;
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.         controller.Move(mov * Time.deltaTime);
  245.  
  246.         RotateLeader(mov);
  247.  
  248.     }
  249.  
  250.  
  251.     public void RotateLeader(Vector3 dir)
  252.     {
  253.         if (dir != Vector3.zero)
  254.         {
  255.             dir = new Vector3(dir.x, 0, dir.z);
  256.             transform.rotation = Quaternion.LookRotation(dir);
  257.  
  258.         }
  259.     }
  260.     public void Boost(float newSpeed, Vector3 newDirection)
  261.     {
  262.         currentSpeed = newSpeed; //Force current speed to launch speed
  263.         maxRunSpeed = newSpeed; //Allow max speed to become launch speed
  264.         controller.Move(newDirection * newSpeed);
  265.         transform.rotation = Quaternion.LookRotation(newDirection); //Force foward rotation to launch direction
  266.     }
  267.  
  268.     public void Launch(Vector3 direction, float height)
  269.     {
  270.         velocity = Mathf.Sqrt(height * -2 * Physics.gravity.y) * direction;
  271.  
  272.     }
  273.  
  274.     private float surrenderDuration;
  275.     private bool surrendered = false;
  276.     public static bool controllable = true;
  277.     public bool trickZone;
  278.  
  279.     /// <summary>
  280.     /// Remove player control for specified time
  281.     /// Force player direction for duration
  282.     /// Use vector2.up for forward movement
  283.     /// </summary>
  284.     /// <param name="direction"></param>
  285.     /// <param name="duration"></param>
  286.     public void SurrenderControl(Vector2 direction, float duration)
  287.     {
  288.         movement = direction; //Direction of movement
  289.  
  290.         surrenderDuration = duration;
  291.  
  292.         surrendered = true;
  293.     }
  294.  
  295.  
  296. }
  297.  
  298.  
  299.  
  300.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement