Hey, there I’m ANSH PARASHAR student of VIT BHOPAL …
So today I’m gonna show you how to make a FIRST PERSON MOVEMENT in UNITY …
First let’s start by adding a TERRAIN. (You can update it later)
But note this while add terrain make sure u add a LAYER from the Inspector window and name it as
(Ground)….
Now Add an EMPTY GAME OBJECT
Make sure the parameters are same as on the INSPECTOR window….
Now as a Child Object Add a Cylinder to this EMPTY GAME Object
Make sure the parameters are same as on the INSPECTOR window….
Again as a Child Object Add a Camera to it ….
Make sure the parameters are same as on the INSPECTOR window….
And Lastly add an EMPTY GAME OBJECT for Ground Check …
Now lets start writing the script
1) For player movement :- Add the script to Parent Game Object……
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity*Time.deltaTime);
}
}
Now in (In Inspector Window) Public Controller Add the parent object
and in the Ground Check add the GroundCheck Child Object …
And in Ground Mask add the layer of {Ground}-(As I told u above to add it to terrain).
2) For Camera Movement :- Attach one Script to the Child Object Camera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
Now in (In Inspector Window) Public Player Body Add the parent object ..
And That’s IT…..U r Done with your First Person Mouse Look.
THANK YOU.....
Here are some references and source where you can download and find the material :
https://learn.unity.com/tutorial/working-with-the-terrain-editor-1#5f1ead89edbc2...
https://docs.unity3d.com/ScriptReference/CharacterController.Move.html