dronkowitz

PlayerMovement.cs Script (Unity) Game = Sonic Heroes

Jan 6th, 2021 (edited)
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     [Header("Movement")]
  8.  
  9.     public int healthPoint = 100;
  10.     private float currentSpeed = 0;
  11.     private float maxRunSpeed = 12;
  12.     private float maxWalkSpeed = 7;
  13.     private bool running;
  14.     private float CurrentMaxSpeed
  15.     {
  16.         get
  17.         {
  18.             if (running) return maxRunSpeed;
  19.             else return maxWalkSpeed;
  20.         }
  21.     }
  22.     private float acceleration = 5;
  23.     private float jumpHeight = 10;
  24.     public bool isJumping = false;
  25.     public bool grounded = false;
  26.     public bool trickZone = false;
  27.     public Vector3 spawnpoint;
  28.     private Vector3 velocity = new Vector3();
  29.     public Vector2 movement = new Vector2();
  30.     public GameObject HUDobject;
  31.     public GameObject Leftpos;
  32.     public GameObject Rightpos;
  33.  
  34.     [Header("Utility")]
  35.     public Rigidbody body;
  36.     public LayerMask groundMask;
  37.     public Transform eyes;
  38.     public Transform characterbody;
  39.  
  40.     // Start is called before the first frame update
  41.     void Start()
  42.     {
  43.         Debug.Log("GameObject");
  44.         body = GetComponent<Rigidbody>();
  45.     }
  46.  
  47.     // Update is called once per frame
  48.     void Update()
  49.     {
  50.         if (Input.GetKeyDown("v"))
  51.         {
  52.             DebugkillSwitch();
  53.         }
  54.  
  55.         if (!surrendered) //If surrended control disable inputs
  56.         {
  57.             movement.x = Input.GetAxis("Horizontal");
  58.             movement.y = Input.GetAxis("Vertical");
  59.  
  60.  
  61.            
  62.         }
  63.  
  64.  
  65.         running = Input.GetKey(KeyCode.LeftShift);
  66.            
  67.        
  68.  
  69.  
  70.         if (Input.GetKeyDown(KeyCode.Space) && grounded)
  71.        
  72.         {
  73.             isJumping = true;
  74.            
  75.         }
  76.         if (currentSpeed > CurrentMaxSpeed)
  77.         {
  78.             currentSpeed = CurrentMaxSpeed;
  79.         }
  80.  
  81.         if (surrendered) //If surrended count down timer and restore control when set to 0
  82.         {
  83.             surrenderDuration -= Time.deltaTime;
  84.             if (surrenderDuration <= 0) surrendered = false;
  85.         }
  86.  
  87.         if (trickZone)
  88.         {
  89.             RaycastHit groundHit;
  90.             if (Physics.Raycast(transform.position, -transform.up, out groundHit, 5))
  91.             {
  92.  
  93.                 Vector3 hitAngle = groundHit.normal;
  94.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(Vector3.Cross(hitAngle, -transform.right)), 360 * Time.deltaTime);
  95.  
  96.             }
  97.             else
  98.             {
  99.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, new Quaternion(0, transform.rotation.y, 0, transform.rotation.w), 360 * Time.deltaTime);
  100.             }
  101.         }
  102.         else
  103.         {
  104.             transform.rotation = Quaternion.RotateTowards(transform.rotation, new Quaternion(0, transform.rotation.y, 0, transform.rotation.w), 360 * Time.deltaTime);
  105.         }
  106.  
  107.     }
  108.  
  109.  
  110.     void FixedUpdate()
  111.     {
  112.  
  113.         MovePlayer(movement.x, movement.y);
  114.        
  115.     }
  116.     void MovePlayer(float moveRight, float moveForward)
  117.     {
  118.         grounded = Physics.CheckSphere(transform.position + (-transform.up * 0.55f), 0.48f, groundMask);
  119.         Vector3 moveDir = transform.forward * moveForward + transform.right * moveRight;
  120.         if (moveDir.sqrMagnitude > 0)
  121.         {
  122.             currentSpeed += acceleration * Time.fixedDeltaTime;
  123.         }
  124.         else
  125.         {
  126.             currentSpeed -= acceleration * Time.fixedDeltaTime * 1.2f;
  127.         }
  128.  
  129.         currentSpeed = Mathf.Clamp(currentSpeed, 0, CurrentMaxSpeed);
  130.  
  131.         moveDir *= currentSpeed;
  132.  
  133.         if (isJumping)
  134.         {
  135.            
  136.             velocity = Mathf.Sqrt(jumpHeight * -2 * Physics.gravity.y) * transform.up;
  137.         }
  138.  
  139.         if (!grounded)
  140.         {
  141.             Leftpos.GetComponent<FollowerNavigation>().Jump(velocity);
  142.             Rightpos.GetComponent<FollowerNavigation>().Jump(velocity);
  143.  
  144.         }
  145.  
  146.         velocity += Physics.gravity.y * Time.fixedDeltaTime * transform.up;
  147.  
  148.         if (!grounded || velocity.y > 0)
  149.         {
  150.             body.velocity = velocity + moveDir * Time.fixedDeltaTime;
  151.         }
  152.         else
  153.         {
  154.             body.velocity = moveDir;
  155.         }
  156.  
  157.         velocity = body.velocity;
  158.  
  159.         Vector3 velocityXZ = velocity;
  160.         velocityXZ.y = 0;
  161.         if(velocityXZ.sqrMagnitude>0.3f)
  162.             characterbody.rotation = Quaternion.LookRotation(velocityXZ);
  163.  
  164.         if (isJumping)
  165.         {
  166.  
  167.             isJumping = false;
  168.            
  169.         }
  170.     }
  171.    
  172.  
  173.  
  174.     public void Launch(Vector3 direction, float height)
  175.     {
  176.         isJumping = false;
  177.         velocity = Mathf.Sqrt(height * -2 * Physics.gravity.y) * direction;
  178.         body.velocity = velocity;
  179.        
  180.     }
  181.  
  182.  
  183.     public void Boost(float newSpeed, Vector3 newDirection)
  184.     {
  185.         currentSpeed = newSpeed; //Force current speed to launch speed
  186.         maxRunSpeed = newSpeed; //Allow max speed to become launch speed
  187.         transform.rotation = Quaternion.LookRotation(newDirection); //Force foward rotation to launch direction
  188.     }
  189.     private float surrenderDuration;
  190.     private bool surrendered = false;
  191.  
  192.     /// <summary>
  193.     /// Remove player control for specified time
  194.     /// Force player direction for duration
  195.     /// Use vector2.up for forward movement
  196.     /// </summary>
  197.     /// <param name="direction"></param>
  198.     /// <param name="duration"></param>
  199.     public void SurrenderControl(Vector2 direction, float duration)
  200.     {
  201.         movement = direction; //Direction of movement
  202.  
  203.         surrenderDuration = duration;
  204.  
  205.         surrendered = true;
  206.     }
  207.  
  208.     public void RestoreControl()
  209.     {
  210.         surrendered = false;
  211.     }
  212.  
  213.     void DebugkillSwitch()
  214.     {
  215.         RespawnPlayer();
  216.     }
  217.  
  218.     public void RespawnPlayer()
  219.     {
  220.         transform.position = spawnpoint;
  221.     }
  222.  
  223. }
Add Comment
Please, Sign In to add comment