Advertisement
kadyr

Untitled

Oct 31st, 2021
1,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     [SerializeField] private float movementSpeed = 10f;
  8.     [SerializeField] private CharacterController characterController;
  9.     [SerializeField] private Animator characterAnimator;
  10.     [SerializeField] Transform cam;
  11.     float turnSmoothTime = 0.1f;
  12.     float turnSmoothVelocity;
  13.  
  14.     void Start()
  15.     {
  16.     }
  17.     void Update()
  18.     {
  19.                 float horizontal = Input.GetAxis("Horizontal");
  20.                 float vertical = Input.GetAxis("Vertical");
  21.                 Vector3 direction = new Vector3(horizontal,0,vertical).normalized;
  22.                 if(direction.magnitude>=0.01f){
  23.                     float targetAngle = Mathf.Atan2(direction.x,direction.z)*Mathf.Rad2Deg + cam.eulerAngles.y;
  24.                     float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y,targetAngle,ref turnSmoothVelocity,turnSmoothTime);
  25.                     transform.rotation = Quaternion.Euler(0,angle,0);
  26.                     Vector3 moveDir = Quaternion.Euler(0,targetAngle,0)*Vector3.forward;
  27.                     characterController.Move(moveDir.normalized*movementSpeed*Time.deltaTime);
  28.                 }
  29.         }
  30.        
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement