Answers for "how to make an player movement in UNITY 2020"

C#
0

how to make an player movement in UNITY 2020

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThirdPersonMovement : MonoBehaviour
{
   //keep in mind that it doesnt have a jump function
   //and that u need to link to the script a player and a camera. Good luck!
   public CharacterController controller;
    public Transform cam;

    public float speed = 6f;

    public float turnSmoothTime = 0.1f;

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical ).normalized;

        if(direction.magnitude >= 0.1f) 
        {
            float tragetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            
             transform.rotation = Quaternion.Euler(0f, tragetAngle, 0f);
         
            
            Vector3 moveDir = Quaternion.Euler(0f, tragetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
           
        }
    }
}
Posted by: Guest on February-20-2021

Code answers related to "how to make an player movement in UNITY 2020"

C# Answers by Framework

Browse Popular Code Answers by Language