Advertisement
Guest User

PlayerController

a guest
Mar 31st, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6.  
  7. public class PlayerController : MonoBehaviour
  8. {
  9. public PlayerGlobal playerGlobal;
  10. public Animator _animator;
  11.  
  12.  
  13. public Transform CameraT;
  14. public Transform Model;
  15.  
  16.  
  17. CharacterController controller;
  18. public float moveSpeed = 4f;
  19. public float yAngle;
  20.  
  21. public float turnSmoothTime = 0.2f;
  22. float turnSmoothVelocity1;
  23. float turnSmoothVelocity2;
  24.  
  25. private void Start()
  26. {
  27. playerGlobal = GetComponentInParent<PlayerGlobal>();
  28. controller = GetComponent<CharacterController>();
  29. }
  30.  
  31. void Update() { if (playerGlobal.AllowPlayerToMove) Movement(); }
  32.  
  33. void Movement()
  34. {
  35. moveSpeed = Input.GetKey(KeyCode.LeftShift) ? 6f : 3f;
  36. Model.position = transform.position;
  37.  
  38. Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  39. Vector2 inputDir = input.normalized;
  40.  
  41. if (inputDir != Vector2.zero)
  42. {
  43. float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + CameraT.eulerAngles.y; //target rotation based on inputs from camera.
  44. transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity1, 0); // logic rotation no smoothing
  45. Model.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(Model.transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity2, turnSmoothTime); // model rotation with smoothing.
  46. }
  47.  
  48. Vector3 velocity = transform.forward * (moveSpeed * inputDir.magnitude) * Time.deltaTime;
  49. controller.Move(velocity);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement