unity player script
A downloadable asset pack
C# Unity Player movement script!
how 2 usee script (insert 2007 music)
STEP 1
make a unity 3d project if you havent yet
STEP 2
make a capsule, and put the Main Camera in it
STEP 3
add a RigidBody to the capsule, and name the capsule whatever you want.
STEP 4
make a C# script:
// some silly player movement made by @its_notalex
// please credit on use! or atleast credit me in the games description wherever your uploading it. thanks for using!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("some silly script made by its_notalex")]
[Header("make sure theres a player with a rigidbody and transform")]
[Header("also make sure theres a camera in the player with transform")]
[Header("settings")]
[Tooltip("camera sensitivity")]
public float Sensitivity = 3f;
[Tooltip("sonic speed")]
public float WalkSpeed = 5f;
[Tooltip("how high do you want to jump? (goes to heaven)")]
public float JumpForce = 5f;
[Tooltip("so like, what is the ground? [Layer Mask]")]
public LayerMask WhatIsGround;
//preparing required stuff
private Rigidbody playerRB;
private Transform cameraTransform;
private Transform orientation;
private Vector3 moveDirection;
private Vector2 turn;
private bool isGrounded;
void Start()
{
// we lockin' the cursor with this one!! 🗣️🔥🔥
Cursor.lockState = CursorLockMode.Locked;
// get required stuff
playerRB = GetComponent<Rigidbody>();
cameraTransform = GetComponentInChildren<Camera>().transform;
orientation = new GameObject("Orientation").transform;
orientation.SetParent(transform);
orientation.localPosition = Vector3.zero;
// the player movement script wont work without this one script
playerRB.constraints = RigidbodyConstraints.FreezeRotation;
}
void Update()
{
// camera stuff
turn.x += Input.GetAxis("Mouse X") * Sensitivity;
turn.y -= Input.GetAxis("Mouse Y") * Sensitivity;
turn.y = Mathf.Clamp(turn.y, -90f, 90f); // remove this if you want for the player to turn anywhere they want and go crazy LOOL (jk i dont recommend)
transform.localRotation = Quaternion.Euler(0, turn.x, 0);
cameraTransform.localRotation = Quaternion.Euler(turn.y, 0, 0);
// movement loool
moveDirection = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
moveDirection += orientation.forward;
}
if (Input.GetKey(KeyCode.S))
{
moveDirection -= orientation.forward;
}
if (Input.GetKey(KeyCode.A))
{
moveDirection -= orientation.right;
}
if (Input.GetKey(KeyCode.D))
{
moveDirection += orientation.right;
}
//stuff to fix some stuff
moveDirection.y = 0;
moveDirection.Normalize();
// the real movement script
playerRB.MovePosition(playerRB.position + moveDirection * WalkSpeed * Time.deltaTime);
// Jump
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
playerRB.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}
customize it however youd like
STEP 5
make a layer mask and a tag called "Ground"
STEP 6
apply the layer mask and tag to all objects that you want for the player to be able to jump on!
STEP 7
attach the script to the capsule and edit the sensitivity and walkspeed and jump force if youd like
STEP 8
congratulations! you have a player in unity now!
keep in mind this script is NOT PERFECT, and its still not finished. so use it or not use it if youd like for your games
Status | In development |
Category | Assets |
Author | notalex |
Leave a comment
Log in with itch.io to leave a comment.