Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- ///
- /// </summary>
- public class PlayerMotor : MonoBehaviour
- {
- ///Attributs
- private const float LANE_DISTANCE = 3.0f;
- private const float TURN_SPEED = 0.05f;
- //
- private bool isRunning = false;
- //Animation
- private Animator anim;
- //Movement
- [SerializeField] CharacterController controller;
- [SerializeField] private float jumpForce=4.0f;
- [SerializeField] private float gravity = 12.0f;
- [SerializeField] private float verticalVelocity;
- [SerializeField]private float speed = 7.0f;
- [SerializeField] private int desiredLane = 1; //0=Left, 1=Middle,2=Right;
- ///Methods
- private void Start()
- {
- controller = GetComponent<CharacterController>();
- anim = GetComponent<Animator>();
- }
- private void Update()
- {
- if (!isRunning)
- return;
- //Gether the inputs on wich lane we should be
- if (MobileInput.Instance.SwipeLeft)
- MoveLane(false);
- if (MobileInput.Instance.SwipeRight)
- MoveLane(true);
- //Calculate where we should be in the future
- Vector3 targetPosition = transform.position.z * Vector3.forward;
- if (desiredLane == 0)
- targetPosition += Vector3.left * LANE_DISTANCE;
- else if (desiredLane == 2)
- targetPosition += Vector3.right * LANE_DISTANCE;
- //Calculate move delta
- Vector3 moveVector = Vector3.zero;
- moveVector.x = (targetPosition - transform.position).normalized.x * speed;
- bool isVarGrounded = isGrounded();
- anim.SetBool("Grounded", isVarGrounded);
- //Calculate Y
- if (isVarGrounded) //If grounded
- {
- verticalVelocity = -0.1f;
- if (MobileInput.Instance.SwipeUp)
- {
- //Jump
- anim.SetTrigger("Jump");
- verticalVelocity = jumpForce;
- }
- else if(MobileInput.Instance.SwipeDown)
- {
- Debug.Log("SwipeDown");
- //Slide
- //startSliding();
- //Invoke("stopSliding", 0.50f);
- }
- }
- else
- {
- verticalVelocity -= (gravity * Time.deltaTime);
- //Fast falling mechanic
- if (MobileInput.Instance.SwipeDown)
- {
- verticalVelocity = -jumpForce;
- }
- }
- moveVector.y = verticalVelocity;
- moveVector.z = speed;
- //Move the character
- controller.Move(moveVector * Time.deltaTime);
- //Rotate the char the way he is going to
- Vector3 dir = controller.velocity;
- if (dir != Vector3.zero)
- {
- dir.y = 0;
- transform.forward = Vector3.Lerp(transform.forward, dir, TURN_SPEED);
- }
- }
- //Move Left or Right
- private void MoveLane(bool goingRight)
- {
- desiredLane += (goingRight) ? 1 : -1;
- desiredLane = Mathf.Clamp(desiredLane, 0, 2);
- }
- private bool isGrounded()
- {
- 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);
- Debug.DrawRay(groundRay.origin, groundRay.direction, Color.cyan, 1.0f);
- return Physics.Raycast(groundRay, 0.2f + 0.1f);
- }
- public void startRunning()
- {
- isRunning = true;
- anim.SetTrigger("StartRunning");
- }
- private void crash()
- {
- anim.SetTrigger("Death");
- isRunning = false;
- }
- private void OnControllerColliderHit(ControllerColliderHit hit)
- {
- switch(hit.gameObject.tag)
- {
- case "Obstacle":
- crash();
- break;
- }
- }
- private void startSliding()
- {
- anim.SetBool("IsSliding", true);
- controller.height /= 2;
- controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
- }
- private void stopSliding()
- {
- anim.SetBool("IsSliding", false);
- controller.height *= 2;
- controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement