0% found this document useful (0 votes)
4 views

Unity Movement Script

Uploaded by

yakshgoyal0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unity Movement Script

Uploaded by

yakshgoyal0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

3 Person View Movement Chart on Unity

1. Variables
csharp
Copy code
public float moveSpeed = 5f;
public float mouseSensitivity = 100f;

private Rigidbody rb;


private Transform cameraTransform;
private float xRotation = 0f;

 moveSpeed: Controls the speed of player movement.


 mouseSensitivity: Controls how fast the player looks around using the mouse.
 rb: Stores a reference to the Rigidbody component for physics-based movement.
 cameraTransform: Holds a reference to the player's camera for rotation control.
 xRotation: Tracks the vertical (up/down) rotation of the camera, ensuring it doesn't
over-rotate.

2. Start Method
csharp
Copy code
void Start()
{
rb = GetComponent<Rigidbody>();
cameraTransform = Camera.main.transform;
Cursor.lockState = CursorLockMode.Locked;
}

 GetComponent<Rigidbody>(): Finds and stores the Rigidbody attached to the player


for movement.
 Camera.main.transform: Gets the main camera's transform to control its rotation.
 Cursor.lockState = CursorLockMode.Locked;: Locks the mouse cursor to the
center of the screen for immersive control and hides it.

3. Update Method
csharp
Copy code
void Update()
{
HandleMouseLook();
HandleMovement();
}

The Update method is called every frame. It calls:


 HandleMouseLook(): Processes mouse input for looking around.
 HandleMovement(): Handles keyboard input for moving the player.

4. Mouse Look Handling


csharp
Copy code
void HandleMouseLook()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity *
Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity *
Time.deltaTime;

xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);

cameraTransform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);


transform.Rotate(Vector3.up * mouseX);
}

 Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y"): Get mouse


movement data on the X and Y axes.
 Time.deltaTime: Ensures the movement is frame rate independent.
 Vertical Rotation (xRotation):
o Adjusts the up/down camera rotation.
o Clamping (Mathf.Clamp): Restricts the vertical rotation between -90° and
90° to prevent over-rotating.
 Horizontal Rotation:
o Rotates the entire player (not just the camera) left/right based on Mouse X.

5. Player Movement Handling


csharp
Copy code
void HandleMovement()
{
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");

Vector3 moveDirection = transform.right * moveX + transform.forward *


moveZ;

Vector3 newVelocity = moveDirection * moveSpeed;


rb.velocity = new Vector3(newVelocity.x, rb.velocity.y, newVelocity.z);
}

 Input.GetAxis("Horizontal") and Input.GetAxis("Vertical"): Capture


WASD or arrow key inputs for movement.
o Horizontal: Left/right movement (A/D or Left/Right arrows).
o Vertical:
Forward/backward movement (W/S or Up/Down arrows).
 Movement Direction:
o transform.right: Represents the player’s right direction.
o transform.forward: Represents the player’s forward direction.
o Combines the two for proper directional movement.
 rb.velocity:
o Assigns a new velocity to the Rigidbody based on the calculated direction.
o Ensures the y-velocity (e.g., gravity) remains unchanged.

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!

You might also like