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 PlayerMovement : MonoBehaviour
- {
- [SerializeField] private float movementSpeed = 10f;
- [SerializeField] private CharacterController characterController;
- [SerializeField] private Animator characterAnimator;
- [SerializeField] Transform cam;
- float turnSmoothTime = 0.1f;
- float turnSmoothVelocity;
- void Start()
- {
- }
- void Update()
- {
- float horizontal = Input.GetAxis("Horizontal");
- float vertical = Input.GetAxis("Vertical");
- Vector3 direction = new Vector3(horizontal,0,vertical).normalized;
- if(direction.magnitude>=0.01f){
- float targetAngle = Mathf.Atan2(direction.x,direction.z)*Mathf.Rad2Deg + cam.eulerAngles.y;
- float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y,targetAngle,ref turnSmoothVelocity,turnSmoothTime);
- transform.rotation = Quaternion.Euler(0,angle,0);
- Vector3 moveDir = Quaternion.Euler(0,targetAngle,0)*Vector3.forward;
- characterController.Move(moveDir.normalized*movementSpeed*Time.deltaTime);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement