Skip to content

Instantly share code, notes, and snippets.

@Pikachuxxxx
Created July 5, 2020 12:01
Show Gist options
  • Save Pikachuxxxx/35887d7190efd55ba9a129532ac6cf18 to your computer and use it in GitHub Desktop.
Save Pikachuxxxx/35887d7190efd55ba9a129532ac6cf18 to your computer and use it in GitHub Desktop.
Simple TPP player movement in Unity
using UnityEngine;
using Cinemachine;
public class PlayerMovement : MonoBehaviour
{
public float lookSpeed = 1.5f;
public float moveSpeed = 5f;
public Camera cam;
private Vector3 moveDirection = Vector3.zero;
private float x;
private float z;
void Start()
{
Application.targetFrameRate = 300;
walkSource = GetComponent<AudioSource>();
walkSource.clip = walkClips[Random.Range(0, walkClips.Length)];
}//Start
private void Update()
{
x = Input.GetAxis(Axis.Horizontal);
z = Input.GetAxis(Axis.Vertical);
GetPlayerDirection();
}//Update
void GetPlayerDirection()
{
moveDirection = cam.transform.right * x + cam.transform.forward * z;
if (moveDirection != Vector3.zero)
{
//Player Rotation
Quaternion rotDir = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x,// X
Quaternion.Slerp(transform.rotation, rotDir, Time.deltaTime * lookSpeed).eulerAngles.y, // Y
transform.rotation.eulerAngles.z)); // Z
//Player Movement
moveDirection = transform.forward * moveSpeed * Time.deltaTime;
}
else
moveDirection = Vector3.zero;
}
}//class PlayerMovement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment