Created
October 27, 2023 20:51
-
-
Save TheMehranKhan/28d13691ee1ba2d66b0b672b5fa52239 to your computer and use it in GitHub Desktop.
This code block provides a basic player movement functionality in Unity. It allows the player to move forward, backward, left, and right using the arrow keys or WASD keys.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: themehrankhan | |
License: MIT License | |
Description: | |
This code block provides a basic player movement functionality in Unity. It allows the player to move forward, backward, left, and right using the arrow keys or WASD keys. | |
Usage: | |
1. Attach this script to the player object in Unity. | |
2. Ensure the player object has a Rigidbody component attached. | |
3. Press the arrow keys or WASD keys to move the player. | |
*/ | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour | |
{ | |
public float speed = 5f; // Speed of player movement | |
private Rigidbody rb; | |
private void Start() | |
{ | |
rb = GetComponent<Rigidbody>(); | |
} | |
private void FixedUpdate() | |
{ | |
float moveHorizontal = Input.GetAxis("Horizontal"); | |
float moveVertical = Input.GetAxis("Vertical"); | |
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical) * speed; | |
rb.velocity = movement; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment