Skip to content

Instantly share code, notes, and snippets.

@ysalihtuncel
Last active April 5, 2025 18:51
Show Gist options
  • Save ysalihtuncel/c6ec74c0b851603683b2613d7e5afed2 to your computer and use it in GitHub Desktop.
Save ysalihtuncel/c6ec74c0b851603683b2613d7e5afed2 to your computer and use it in GitHub Desktop.
PlayerMovement
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float sensivity = 1f;
public Transform orientation;
float xRotation = 0f;
float yRotation = 0f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * sensivity;
float mouseY = Input.GetAxis("Mouse Y") * Time.deltaTime * sensivity;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
yRotation += mouseX;
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0f);
orientation.rotation = Quaternion.Euler(0f, yRotation, 0f);
}
}
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
public Transform cameraPosition;
// Update is called once per frame
void Update()
{
transform.position = cameraPosition.position;
}
}
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed = 5f;
public float groundDrag;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump = true;
[Header("Ground Check")]
public float playerHeight = 1.8f;
public LayerMask groundLayer;
bool grounded;
[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb => GetComponent<Rigidbody>();
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb.freezeRotation = true;
}
// Update is called once per frame
void Update()
{
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * .5f + 0.2f, groundLayer);
MyInput();
SpeedControl();
rb.linearDamping = grounded ? groundDrag : 0;
}
void FixedUpdate()
{
MovePlayer();
}
void MyInput()
{
horizontalInput = Input.GetAxis("Horizontal"); // -1..0..1
verticalInput = Input.GetAxis("Vertical");
if (Input.GetKey(jumpKey) && readyToJump && grounded)
{
Jump();
readyToJump = false;
Invoke(nameof(ResetJump), jumpCooldown);
}
}
void MovePlayer()
{
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
if (grounded)
rb.AddForce(moveDirection * moveSpeed * 10f, ForceMode.Force);
else
rb.AddForce(moveDirection * moveSpeed * 10f * airMultiplier, ForceMode.Force);
}
void SpeedControl()
{
Vector3 flatVelocity = new (rb.linearVelocity.x, 0, rb.linearVelocity.z);
if (flatVelocity.magnitude > moveSpeed)
{
flatVelocity = flatVelocity.normalized * moveSpeed;
rb.linearVelocity = new Vector3(flatVelocity.x, rb.linearVelocity.y, flatVelocity.z);
}
}
void Jump()
{
rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0, rb.linearVelocity.z);
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
void ResetJump()
{
readyToJump = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment