0% found this document useful (0 votes)
10 views1 page

Code Csharp

This Unity script controls player movement using Rigidbody physics. It allows the player to move within defined boundary limits on the X and Z axes. The movement speed is adjustable, and the player's position is clamped to prevent moving outside the specified boundaries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Code Csharp

This Unity script controls player movement using Rigidbody physics. It allows the player to move within defined boundary limits on the X and Z axes. The movement speed is adjustable, and the player's position is clamped to prevent moving outside the specified boundaries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

using UnityEngine;

public class PlayerMovement : MonoBehaviour


{
public float moveSpeed = 5f; // Speed of the player movement
private Rigidbody rb; // Reference to the Rigidbody component

// Define boundary limits


public float boundaryX = 5f; // X boundary limit
public float boundaryZ = 5f; // Z boundary limit

void Start()
{
rb = GetComponent<Rigidbody>();
}

void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);


rb.MovePosition(transform.position + movement * moveSpeed *
Time.deltaTime);

// Restrict movement within boundaries


Vector3 clampedPosition = transform.position;
clampedPosition.x = Mathf.Clamp(clampedPosition.x, -boundaryX, boundaryX);
clampedPosition.z = Mathf.Clamp(clampedPosition.z, -boundaryZ, boundaryZ);
transform.position = clampedPosition;
}
}

You might also like