Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6. public class ThirdPersonController : MonoBehaviour
  7. {
  8.  
  9. CharacterController characterController;
  10. public float rotationSpeed = 60.0f;
  11. public float walkingSpeed = 8.0f;
  12. public float mouseSensitivity = 20;
  13. public float jumpSpeed = 8.0f;
  14. public float airSpeed = 5f;
  15. public float gravity = 9.81f;
  16.  
  17. Vector3 velocity = Vector3.zero;
  18.  
  19. void Start ()
  20. {
  21. characterController = this.GetComponent<CharacterController>();
  22. }
  23.  
  24.  
  25. void Update ()
  26. {
  27. float forward = Input.GetAxis("Vertical");
  28. float rotate = Input.GetAxis("Horizontal");
  29. float strafe = Input.GetAxis("Strafe");
  30.  
  31. if (Input.GetMouseButton(1))
  32. {
  33. rotate = Input.GetAxis("Mouse X") * mouseSensitivity;
  34. }
  35.  
  36. Vector3 airVelocity = Vector3.zero;
  37.  
  38. if (characterController.isGrounded)
  39. {
  40. velocity = this.transform.right * walkingSpeed * strafe + this.transform.forward * walkingSpeed * forward;
  41.  
  42. if (Input.GetButton("Jump"))
  43. {
  44. velocity.y = jumpSpeed;
  45. }
  46. }
  47. else
  48. {
  49. airVelocity = forward * airSpeed * this.transform.forward + strafe * airSpeed * this.transform.right;
  50. }
  51.  
  52. velocity.y -= gravity * Time.deltaTime;
  53.  
  54. characterController.Move((velocity + airVelocity) * Time.deltaTime);
  55. transform.Rotate(Vector3.up, rotate * Time.deltaTime);
  56.  
  57. transform.Rotate(Vector3.up, rotate * rotationSpeed * Time.deltaTime);
  58. Vector3 direction = this.transform.right * strafe + this.transform.forward * forward;
  59. characterController.SimpleMove(direction * walkingSpeed);
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement