Skip to content

Instantly share code, notes, and snippets.

@TheMehranKhan
Created October 27, 2023 20:51
Show Gist options
  • Save TheMehranKhan/28d13691ee1ba2d66b0b672b5fa52239 to your computer and use it in GitHub Desktop.
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.
/*
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