Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(CharacterController))]
- public class SC_CharacterController : MonoBehaviour
- {
- public float speed = 7.5f;
- public float jumpSpeed = 8.0f;
- public float gravity = 20.0f;
- public float lookSpeed = 2.0f;
- public float lookXLimit = 45.0f;
- public Camera playerCamera;
- CharacterController characterController;
- Vector3 moveDirection = Vector3.zero;
- Vector2 rotation = Vector2.zero;
- [HideInInspector]
- public bool canMove = true;
- void Start()
- {
- characterController = GetComponent<CharacterController>();
- rotation.y = transform.eulerAngles.y;
- }
- void Update()
- {
- if (characterController.isGrounded)
- {
- Vector3 _foward = transform.TransformDirection(Vector3.forward);
- Vector3 _right = transform.TransformDirection(Vector3.right);
- float _horizontal = Input.GetAxis("Horizontal");
- float _vertical = Input.GetAxis("Vertical");
- float curSpeedX = canMove ? speed * _vertical : 0;
- float curSpeedY = canMove ? speed * _horizontal : 0;
- moveDirection = (_foward * curSpeedX) + (_right * curSpeedY);
- if (Input.GetButton("Jump") && canMove)
- {
- moveDirection.y = jumpSpeed;
- }
- }
- moveDirection.y -= gravity * Time.deltaTime;
- characterController.Move(moveDirection * Time.deltaTime);
- if (canMove)
- {
- float _mouseX = Input.GetAxis("Mouse X");
- float _mouseY = Input.GetAxis("Mouse Y");
- rotation.y += _mouseX * lookSpeed;
- rotation.x += -_mouseY * lookSpeed;
- rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
- playerCamera.transform.localRotation = Quaternion.Euler(rotation.x, 0, 0);
- transform.eulerAngles = new Vector2(0, rotation.y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement