how to add movement in unity 3d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//this is for first person movement
public CharacterController controller;
public float speed = 12f;
Vector3 velocity;
public float gravity = -9.81f;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public Transform groundCheck;
bool isGrounded;
float jumpheight = 5f;
// Update is called once per frame
void Update()
{
//movement
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
//gravity
velocity.y += gravity * Time.deltaTime;
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
controller.Move(velocity * Time.deltaTime);
if(isGrounded && velocity.y >= 0)
{
velocity.y = -2f;
}
//jumping
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
}
//sprinting
if(Input.GetKeyDown(KeyCode.LeftShift))
{
speed = 20f;
}
else
{
speed = 12f;
}
}
}