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

PlayerMovementTutorial Cs

This C# script controls player movement in a Unity game. It gets input from the player, checks if the player is grounded, and applies forces to the player's rigidbody to move and jump. It handles movement on the ground differently than in air, applying drag when grounded and limiting speed. It also includes a cooldown for jumping to prevent continuous jumping.

Uploaded by

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

PlayerMovementTutorial Cs

This C# script controls player movement in a Unity game. It gets input from the player, checks if the player is grounded, and applies forces to the player's rigidbody to move and jump. It handles movement on the ground differently than in air, applying drag when grounded and limiting speed. It also includes a cooldown for jumping to prevent continuous jumping.

Uploaded by

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

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class PlayerMovementTutorial : MonoBehaviour


{
[Header("Movement")]
public float moveSpeed;

public float groundDrag;

public float jumpForce;


public float jumpCooldown;
public float airMultiplier;
bool readyToJump;

[HideInInspector] public float walkSpeed;


[HideInInspector] public float sprintSpeed;

[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;

[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;

public Transform orientation;

float horizontalInput;
float verticalInput;

Vector3 moveDirection;

Rigidbody rb;

private void Start()


{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;

readyToJump = true;
}

private void Update()


{
// ground check
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight *
0.5f + 0.3f, whatIsGround);

MyInput();
SpeedControl();

// handle drag
if (grounded)
rb.drag = groundDrag;
else
rb.drag = 0;
}

private void FixedUpdate()


{
MovePlayer();
}

private void MyInput()


{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");

// when to jump
if(Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;

Jump();

Invoke(nameof(ResetJump), jumpCooldown);
}
}

private void MovePlayer()


{
// calculate movement direction
moveDirection = orientation.forward * verticalInput + orientation.right *
horizontalInput;

// on ground
if(grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f,
ForceMode.Force);

// in air
else if(!grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier,
ForceMode.Force);
}

private void SpeedControl()


{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

// limit velocity if needed


if(flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
}

private void Jump()


{
// reset y velocity
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);


}
private void ResetJump()
{
readyToJump = true;
}
}

You might also like