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

unity

Uploaded by

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

unity

Uploaded by

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

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 = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}

void FixedUpdate()
{
// Move the player based on the input
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
|||||||||||||||||||| 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 = KeyCode.Space; // Key for jumping
public KeyCode runKey = KeyCode.LeftShift; // Key for running
public KeyCode slideKey = KeyCode.LeftControl; // 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 = Physics2D.OverlapCircle(groundCheck.position, 0.1f,
groundMask);

// Handle input for movement


float moveInput = Input.GetAxisRaw("Horizontal");

// Check if the player wants to run


isRunning = Input.GetKey(runKey);
// Check if the player wants to slide
isSliding = Input.GetKeyDown(slideKey) && isGrounded;

// Apply movement force


float speed = isRunning ? runSpeed : walkSpeed;
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

// Handle jumping
if (Input.GetKeyDown(jumpKey) && isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}

// Handle sliding
if (isSliding)
{
rb.AddForce(Vector2.right * slideForce * transform.localScale.x,
ForceMode2D.Impulse);
}
}
}

You might also like