Advertisement
Guest User

CharacterController

a guest
Feb 25th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6.  
  7. public class SC_CharacterController : MonoBehaviour
  8. {
  9.  
  10.     public float speed = 7.5f;
  11.     public float jumpSpeed = 8.0f;
  12.     public float gravity = 20.0f;
  13.     public float lookSpeed = 2.0f;
  14.     public float lookXLimit = 45.0f;
  15.  
  16.     public Camera playerCamera;
  17.     CharacterController characterController;
  18.     Vector3 moveDirection = Vector3.zero;
  19.     Vector2 rotation = Vector2.zero;
  20.  
  21.     [HideInInspector]
  22.     public bool canMove = true;
  23.  
  24.     void Start()
  25.     {
  26.         characterController = GetComponent<CharacterController>();
  27.         rotation.y = transform.eulerAngles.y;
  28.     }
  29.  
  30.     void Update()
  31.     {
  32.         if (characterController.isGrounded)
  33.         {
  34.  
  35.             Vector3 _foward = transform.TransformDirection(Vector3.forward);
  36.             Vector3 _right = transform.TransformDirection(Vector3.right);
  37.             float _horizontal = Input.GetAxis("Horizontal");
  38.             float _vertical = Input.GetAxis("Vertical");
  39.  
  40.             float curSpeedX = canMove ? speed * _vertical : 0;
  41.             float curSpeedY = canMove ? speed * _horizontal : 0;
  42.  
  43.             moveDirection = (_foward * curSpeedX) + (_right * curSpeedY);
  44.  
  45.             if (Input.GetButton("Jump") && canMove)
  46.             {
  47.                 moveDirection.y = jumpSpeed;
  48.             }
  49.  
  50.         }
  51.  
  52.         moveDirection.y -= gravity * Time.deltaTime;
  53.  
  54.         characterController.Move(moveDirection * Time.deltaTime);
  55.  
  56.         if (canMove)
  57.         {
  58.  
  59.             float _mouseX = Input.GetAxis("Mouse X");
  60.             float _mouseY = Input.GetAxis("Mouse Y");
  61.  
  62.             rotation.y += _mouseX * lookSpeed;
  63.             rotation.x += -_mouseY * lookSpeed;
  64.             rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
  65.             playerCamera.transform.localRotation = Quaternion.Euler(rotation.x, 0, 0);
  66.             transform.eulerAngles = new Vector2(0, rotation.y);
  67.  
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement