Last active
August 13, 2023 21:51
-
-
Save wipermail/71c1b0b0153dd0e548b5877d56613a0e to your computer and use it in GitHub Desktop.
Framework movement Horizontal/Vertical - Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(CharacterController))] | |
[AddComponentMenu("Custom/Control Script/FPS Input")] | |
public class FPSInput : MonoBehaviour | |
{ | |
private CharacterController _characterController; | |
public float speed = 6.0f; | |
public float gravity = -9.8f; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
_characterController = GetComponent<CharacterController>(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
float deltaX = Input.GetAxis("Horizontal") * speed; | |
float deltaZ = Input.GetAxis("Vertical") * speed; | |
Vector3 movement = new Vector3(deltaX, 0, deltaZ); | |
movement = Vector3.ClampMagnitude(movement, speed); | |
movement.y = gravity; | |
movement *= Time.deltaTime; | |
movement = transform.TransformDirection(movement); | |
_characterController.Move(movement); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment