Unity Movement Script
Unity Movement Script
1. Variables
csharp
Copy code
public float moveSpeed = 5f;
public float mouseSensitivity = 100f;
2. Start Method
csharp
Copy code
void Start()
{
rb = GetComponent<Rigidbody>();
cameraTransform = Camera.main.transform;
Cursor.lockState = CursorLockMode.Locked;
}
3. Update Method
csharp
Copy code
void Update()
{
HandleMouseLook();
HandleMovement();
}
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
6. Key Features
1. Physics-Based Movement:
o Uses Rigidbody for smooth physics interactions.
2. Mouse Look:
o Allows both horizontal and vertical rotation with proper limits.
3. Frame-Rate Independence:
o Uses Time.deltaTime to make input responsiveness consistent regardless of
the frame rate.
Improvements to Consider
1. Jumping: Add a Jump mechanic by checking for spacebar input and applying upward
force to the Rigidbody.
2. Smoother Camera Control: Introduce a smoothing factor for the mouse movement
to make camera control more fluid.
3. Run/Sprint: Include a key (like Shift) to increase the moveSpeed temporarily.
Let me know if you’d like help with adding any of these features!