Skip to content

Instantly share code, notes, and snippets.

@wipermail
Last active August 13, 2023 21:51
Show Gist options
  • Save wipermail/71c1b0b0153dd0e548b5877d56613a0e to your computer and use it in GitHub Desktop.
Save wipermail/71c1b0b0153dd0e548b5877d56613a0e to your computer and use it in GitHub Desktop.
Framework movement Horizontal/Vertical - Unity
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