Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Video;
- public class PlayerController : MonoBehaviour {
- private Animator animator;
- public bool inclineTilting = true;
- private float walkingSpeed = 5f;
- private float runningSpeed = 9f;
- private float currentSpeed;
- private float targetSpeed;
- CharacterController characterController;
- public float gravity = -20f;
- private bool running;
- private float turnSmoothTime = 0.1f;
- private float turnSmoothVelocity;
- private float targetRotation;
- private float turnSmoothSpeed = 15f;
- private float angularSpeed = 4.0f;
- private float speedSmoothTime = 0.05f;
- private float runningSpeedSmoothTime = 0.05f;
- private float runningSpeedVelocity;
- private float velocityY;
- private float jumpHeight = 10f;
- private bool isFirstJump = true;
- private bool landing = false;
- private Quaternion quatTargetRotation;
- // Use this for initialization
- void Start () {
- characterController = GetComponent<CharacterController>();
- animator = GetComponent<Animator>();
- }
- // Update is called once per frame
- void Update () {
- HandleMovement();
- HandleJump();
- }
- void HandleJump(){
- if(Input.GetButtonDown("Jump")){ // first jump
- if(isFirstJump){
- animator.SetBool("jump", true);
- velocityY = Mathf.Sqrt(-2 * gravity * jumpHeight);
- isFirstJump = !isFirstJump;
- }else if(!isFirstJump && animator.GetBool("jump")){
- velocityY = Mathf.Sqrt(-2 * gravity * jumpHeight * 1.5f);
- animator.SetBool("jump", false);
- animator.SetBool("secondJump", true);
- }
- }
- GroundedCheck();
- }
- private void GroundedCheck() {
- RaycastHit hit;
- Vector3 ray = transform.TransformDirection(Vector3.down);
- if(characterController.isGrounded || Physics.Raycast(transform.position, ray, 2.0f)) {
- animator.SetBool("onAir", false);
- }else {
- animator.SetBool("onAir", true);
- }
- }
- void HandleMovement(){
- Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
- Vector2 inputDir = input.normalized;
- // transform.eulerAngles = Vector3.up * Camera.main.transform.eulerAngles.y;
- if(inputDir != Vector2.zero){
- float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.y;
- transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
- }
- if(!characterController.isGrounded){
- velocityY += Time.deltaTime * gravity;
- }
- // Debug.Log(velocityY);
- running = Input.GetKey(KeyCode.LeftShift);
- if(!animator.GetBool("attacking")){
- if(!landing){
- targetSpeed = (running ? runningSpeed : walkingSpeed) * inputDir.magnitude;
- }else{
- targetSpeed = 0.0f;
- }
- }else{
- targetSpeed = 0.0f;
- }
- InclineTilt(inputDir);
- currentSpeed = Mathf.SmoothDampAngle(currentSpeed, targetSpeed, ref runningSpeedVelocity, runningSpeedSmoothTime);
- Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
- characterController.Move(velocity * Time.deltaTime);
- animator.SetFloat("speedPercent", (running ? 1f : 0.5f) * inputDir.magnitude, speedSmoothTime, Time.deltaTime);
- }
- private void InclineTilt(Vector2 inputDir) {
- if(inclineTilting && inputDir != Vector2.zero) {
- RaycastHit hit;
- Vector3 ray = transform.TransformDirection(Vector3.down);
- if(Physics.Raycast(transform.position, ray, out hit)) {
- quatTargetRotation = Quaternion.FromToRotation(Vector3.up, hit.normal) * transform.rotation;
- transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.FromToRotation(Vector3.up, hit.normal), 0.5f * Time.deltaTime);
- }
- }
- }
- public void Landed(){
- animator.SetBool("jump", false);
- animator.SetBool("secondJump", false);
- landing = false;
- isFirstJump = true;
- }
- public void MidAirJumpCompleted(){
- animator.SetBool("secondJump", false);
- }
- public void LandingStarts(){
- Debug.Log("landing");
- landing = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement