unity player controller 3d in csharp

Here is an example for creating a player controller in a 3D Unity game using C#:

main.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 10.0f;
    public float jumpForce = 500.0f;
    Rigidbody rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        // Get input from user for movement
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        // Move the player in the direction of input
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rb.AddForce(movement * speed);

        // Check for jump input
        if (Input.GetButtonDown("Jump"))
        {
            // Add upward force to player
            rb.AddForce(Vector3.up * jumpForce);
        }
    }
}
924 chars
36 lines

You would attach this script to the player object in the Unity editor and adjust the various parameters as necessary for your game. This script allows the player to move in the x and z directions using the arrow or WASD keys and jump using the space bar.

gistlibby LogSnag