Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public PlayerGlobal playerGlobal;
- public Animator _animator;
- public Transform CameraT;
- public Transform Model;
- CharacterController controller;
- public float moveSpeed = 4f;
- public float yAngle;
- public float turnSmoothTime = 0.2f;
- float turnSmoothVelocity1;
- float turnSmoothVelocity2;
- private void Start()
- {
- playerGlobal = GetComponentInParent<PlayerGlobal>();
- controller = GetComponent<CharacterController>();
- }
- void Update() { if (playerGlobal.AllowPlayerToMove) Movement(); }
- void Movement()
- {
- moveSpeed = Input.GetKey(KeyCode.LeftShift) ? 6f : 3f;
- Model.position = transform.position;
- Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
- Vector2 inputDir = input.normalized;
- if (inputDir != Vector2.zero)
- {
- float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + CameraT.eulerAngles.y; //target rotation based on inputs from camera.
- transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity1, 0); // logic rotation no smoothing
- Model.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(Model.transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity2, turnSmoothTime); // model rotation with smoothing.
- }
- Vector3 velocity = transform.forward * (moveSpeed * inputDir.magnitude) * Time.deltaTime;
- controller.Move(velocity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement