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 = [Link];
[Link] = [Link];
}
GetComponent<Rigidbody>(): Finds and stores the Rigidbody attached to the player
for movement.
[Link]: Gets the main camera's transform to control its rotation.
[Link] = [Link];: 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 = [Link]("Mouse X") * mouseSensitivity *
[Link];
float mouseY = [Link]("Mouse Y") * mouseSensitivity *
[Link];
xRotation -= mouseY;
xRotation = [Link](xRotation, -90f, 90f);
[Link] = [Link](xRotation, 0f, 0f);
[Link]([Link] * mouseX);
}
[Link]("Mouse X") and [Link]("Mouse Y"): Get mouse
movement data on the X and Y axes.
[Link]: Ensures the movement is frame rate independent.
Vertical Rotation (xRotation):
o Adjusts the up/down camera rotation.
o Clamping ([Link]): 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 = [Link]("Horizontal");
float moveZ = [Link]("Vertical");
Vector3 moveDirection = [Link] * moveX + [Link] *
moveZ;
Vector3 newVelocity = moveDirection * moveSpeed;
[Link] = new Vector3(newVelocity.x, [Link].y, newVelocity.z);
}
[Link]("Horizontal") and [Link]("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 [Link]: Represents the player’s right direction.
o [Link]: Represents the player’s forward direction.
o Combines the two for proper directional movement.
[Link]:
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 [Link] 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!