Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class RBPuppetController : MonoBehaviour
- {
- [SerializeField] private float RunSpeed = 4;
- [SerializeField] private float walkSpeed = 4;
- [SerializeField] private float gravity = -12;
- [SerializeField] private float jumpHeight = 1;
- [SerializeField] private float turnSmoothTime = 0.2f;
- [SerializeField] private float turnSmoothVelocity;
- [SerializeField] private float speedSmoothTime = 0.1f;
- [SerializeField] private float speedSmoothVelocity;
- [SerializeField] private bool lockCursor;
- float velocityY;
- float currentSpeed;
- Transform cameraT;
- Animator animator;
- CharacterController controller;
- // Start is called before the first frame update
- void Start()
- {
- cameraT = Camera.main.transform;
- animator = GetComponent<Animator>();
- controller = GetComponent<CharacterController>();
- if (lockCursor)
- {
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- }
- // Update is called once per frame
- void Update()
- {
- //inputAssignments --- Is it wiser to call buttons within Update to execute functions rather than within the functions themselves?
- Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
- Vector2 inputDir = input.normalized;
- bool running = Input.GetButton("Sprint");
- PlayerMovement(inputDir, running);
- if (Input.GetButton("Jump"))
- {
- Jump();
- }
- //Animation Control
- float animationSpeedPercent = ((running) ? currentSpeed / RunSpeed : currentSpeed / walkSpeed);
- animator.SetFloat("RunBlendSpeed", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
- }
- void PlayerMovement(Vector2 inputDir, bool running)
- {
- if (inputDir != Vector2.zero)
- {
- //Calculates the character rotation using the magic, of TRIG
- float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
- transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
- }
- float targetSpeed = ((running) ? walkSpeed : RunSpeed) * inputDir.magnitude;
- currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
- velocityY = Time.deltaTime * gravity;
- Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
- controller.Move(velocity * Time.deltaTime);
- currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
- if (controller.isGrounded)
- {
- velocityY = 0;
- }
- }
- void Jump()
- {
- if (controller.isGrounded)
- {
- float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
- velocityY = jumpVelocity;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement