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 ThirdPersonController : MonoBehaviour
- {
- CharacterController characterController;
- public float rotationSpeed = 60.0f;
- public float walkingSpeed = 8.0f;
- public float mouseSensitivity = 20;
- public float jumpSpeed = 8.0f;
- public float airSpeed = 5f;
- public float gravity = 9.81f;
- Vector3 velocity = Vector3.zero;
- void Start ()
- {
- characterController = this.GetComponent<CharacterController>();
- }
- void Update ()
- {
- float forward = Input.GetAxis("Vertical");
- float rotate = Input.GetAxis("Horizontal");
- float strafe = Input.GetAxis("Strafe");
- if (Input.GetMouseButton(1))
- {
- rotate = Input.GetAxis("Mouse X") * mouseSensitivity;
- }
- Vector3 airVelocity = Vector3.zero;
- if (characterController.isGrounded)
- {
- velocity = this.transform.right * walkingSpeed * strafe + this.transform.forward * walkingSpeed * forward;
- if (Input.GetButton("Jump"))
- {
- velocity.y = jumpSpeed;
- }
- }
- else
- {
- airVelocity = forward * airSpeed * this.transform.forward + strafe * airSpeed * this.transform.right;
- }
- velocity.y -= gravity * Time.deltaTime;
- characterController.Move((velocity + airVelocity) * Time.deltaTime);
- transform.Rotate(Vector3.up, rotate * Time.deltaTime);
- transform.Rotate(Vector3.up, rotate * rotationSpeed * Time.deltaTime);
- Vector3 direction = this.transform.right * strafe + this.transform.forward * forward;
- characterController.SimpleMove(direction * walkingSpeed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement