Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class PlayerMotor : MonoBehaviour
  10. {
  11.     ///Attributs
  12.     private const float LANE_DISTANCE = 3.0f;
  13.     private const float TURN_SPEED = 0.05f;
  14.  
  15.     //
  16.     private bool isRunning = false;
  17.     //Animation
  18.     private Animator anim;
  19.  
  20.     //Movement
  21.     [SerializeField] CharacterController controller;
  22.     [SerializeField] private   float jumpForce=4.0f;
  23.     [SerializeField] private float gravity = 12.0f;
  24.     [SerializeField] private float verticalVelocity;
  25.     [SerializeField]private float speed = 7.0f;
  26.     [SerializeField] private int desiredLane = 1; //0=Left, 1=Middle,2=Right;
  27.  
  28.     ///Methods
  29.     private void Start()
  30.     {
  31.         controller = GetComponent<CharacterController>();
  32.         anim = GetComponent<Animator>();
  33.     }
  34.     private void Update()
  35.     {
  36.         if (!isRunning)
  37.             return;
  38.         //Gether the inputs on wich lane we should be
  39.         if (MobileInput.Instance.SwipeLeft)
  40.             MoveLane(false);
  41.         if (MobileInput.Instance.SwipeRight)
  42.             MoveLane(true);
  43.  
  44.         //Calculate where we should be in the future
  45.         Vector3 targetPosition = transform.position.z * Vector3.forward;
  46.         if (desiredLane == 0)
  47.             targetPosition += Vector3.left * LANE_DISTANCE;
  48.         else if (desiredLane == 2)
  49.             targetPosition += Vector3.right * LANE_DISTANCE;
  50.  
  51.         //Calculate move delta
  52.         Vector3 moveVector = Vector3.zero;
  53.         moveVector.x = (targetPosition - transform.position).normalized.x * speed;
  54.  
  55.         bool isVarGrounded = isGrounded();
  56.         anim.SetBool("Grounded", isVarGrounded);
  57.         //Calculate Y
  58.         if (isVarGrounded) //If grounded
  59.         {
  60.             verticalVelocity = -0.1f;
  61.            
  62.             if (MobileInput.Instance.SwipeUp)
  63.             {
  64.                 //Jump
  65.                 anim.SetTrigger("Jump");
  66.                 verticalVelocity = jumpForce;
  67.             }
  68.             else if(MobileInput.Instance.SwipeDown)
  69.             {
  70.                 Debug.Log("SwipeDown");
  71.                 //Slide
  72.                 //startSliding();
  73.                 //Invoke("stopSliding", 0.50f);
  74.                
  75.             }
  76.         }
  77.         else
  78.         {
  79.             verticalVelocity -= (gravity * Time.deltaTime);
  80.             //Fast falling mechanic
  81.             if (MobileInput.Instance.SwipeDown)
  82.             {
  83.                 verticalVelocity = -jumpForce;
  84.             }
  85.         }
  86.        
  87.         moveVector.y = verticalVelocity;
  88.         moveVector.z = speed;
  89.  
  90.         //Move the character
  91.         controller.Move(moveVector * Time.deltaTime);
  92.  
  93.         //Rotate the char the way he is going to
  94.         Vector3 dir = controller.velocity;
  95.         if (dir != Vector3.zero)
  96.         {
  97.             dir.y = 0;
  98.             transform.forward = Vector3.Lerp(transform.forward, dir, TURN_SPEED);
  99.         }
  100.        
  101.     }
  102.  
  103.    
  104.     //Move Left or Right
  105.     private void MoveLane(bool goingRight)
  106.     {
  107.         desiredLane += (goingRight) ? 1 : -1;
  108.         desiredLane = Mathf.Clamp(desiredLane, 0, 2);
  109.     }
  110.     private bool isGrounded()
  111.     {
  112.         Ray groundRay=new Ray(new Vector3(controller.bounds.center.x,(controller.bounds.center.y-controller.bounds.extents.y)+0.2f,controller.bounds.center.z),Vector3.down);
  113.         Debug.DrawRay(groundRay.origin, groundRay.direction, Color.cyan, 1.0f);
  114.        
  115.         return Physics.Raycast(groundRay, 0.2f + 0.1f);
  116.     }
  117.     public void startRunning()
  118.     {
  119.         isRunning = true;
  120.         anim.SetTrigger("StartRunning");
  121.     }
  122.  
  123.     private void crash()
  124.     {
  125.         anim.SetTrigger("Death");
  126.         isRunning = false;
  127.     }
  128.     private void OnControllerColliderHit(ControllerColliderHit hit)
  129.     {
  130.         switch(hit.gameObject.tag)
  131.         {
  132.             case "Obstacle":
  133.                 crash();
  134.                 break;
  135.  
  136.         }
  137.     }
  138.  
  139.     private void startSliding()
  140.     {
  141.         anim.SetBool("IsSliding", true);
  142.         controller.height /= 2;
  143.         controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
  144.  
  145.     }
  146.  
  147.     private void stopSliding()
  148.     {
  149.         anim.SetBool("IsSliding", false);
  150.         controller.height *= 2;
  151.         controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement