Nothing really matched my issue
I’m new to controller and started using unity early this year so could this be beginner friendly because I don’t make games very often. I need help making unity detect my buttons I’m pressing on my controller. I’ve got the movement set up though.
This is what my code looks like:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f; // Movement speed
public float lookSpeedX = 2f; // Mouse look speed on the X-axis (horizontal)
public float lookSpeedY = 2f; // Mouse look speed on the Y-axis (vertical)
public bool Sprint = false;
public Camera playerCamera;
private float rotationX = 0f;
void Start()
{
// Get the camera attached to the player object
playerCamera = GetComponentInChildren<Camera>();
// Lock and hide the cursor to prevent it from leaving the game window
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
// First, handle mouse look
float mouseX = Input.GetAxis("Mouse X") * lookSpeedX;
float mouseY = Input.GetAxis("Mouse Y") * lookSpeedY;
// Rotate player horizontally based on mouseX
transform.Rotate(Vector3.up * mouseX);
// Rotate camera vertically based on joystick, but limit the up/down rotation
rotationX -= mouseY;
rotationX = Mathf.Clamp(rotationX, -75f, 75f); // Prevent camera from flipping upside down
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
// Then handle movement (WASD)
float moveX = Input.GetAxis("Horizontal"); // Left/Right
float moveZ = Input.GetAxis("Vertical"); // Forward/Backward
Vector3 moveDirection = transform.right * moveX + transform.forward * moveZ;
// Apply the movement in the world space
transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.World);
}
}