using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f; // Speed of the player movement
public Rigidbody2D rb; // Reference to the player's Rigidbody component
Vector2 movement; // Vector to store player's movement direction
void Update()
{
// Input from the player
movement.x = [Link]("Horizontal");
movement.y = [Link]("Vertical");
}
void FixedUpdate()
{
// Move the player based on the input
[Link]([Link] + movement * moveSpeed * [Link]);
}
}
|||||||||||||||||||| PROJECT-2|||||||||||||||||||||||||
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float walkSpeed = 5f; // Speed of walking
public float runSpeed = 10f; // Speed of running
public float jumpForce = 10f; // Force applied when jumping
public float slideForce = 100f; // Force applied when sliding
public Transform groundCheck; // Transform to check if the player is grounded
public LayerMask groundMask; // Layer mask for the ground
public KeyCode jumpKey = [Link]; // Key for jumping
public KeyCode runKey = [Link]; // Key for running
public KeyCode slideKey = [Link]; // Key for sliding
private Rigidbody2D rb; // Reference to the player's Rigidbody component
private bool isGrounded; // Flag to track if the player is grounded
private bool isRunning; // Flag to track if the player is running
private bool isSliding; // Flag to track if the player is sliding
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// Check if the player is grounded
isGrounded = [Link]([Link], 0.1f,
groundMask);
// Handle input for movement
float moveInput = [Link]("Horizontal");
// Check if the player wants to run
isRunning = [Link](runKey);
// Check if the player wants to slide
isSliding = [Link](slideKey) && isGrounded;
// Apply movement force
float speed = isRunning ? runSpeed : walkSpeed;
[Link] = new Vector2(moveInput * speed, [Link].y);
// Handle jumping
if ([Link](jumpKey) && isGrounded)
{
[Link] = new Vector2([Link].x, jumpForce);
}
// Handle sliding
if (isSliding)
{
[Link]([Link] * slideForce * [Link].x,
[Link]);
}
}
}