how to add movement in unity 3d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Transform cam;
public CharacterController controller;
public float speed = 6f;
public float turnSmoothTime = 0.4f;
public float jumpForce = 10f;
float turnSmoothVelocity;
public LayerMask groundMask;
public float groundDistance = 0.4f;
public Transform groundCheck;
bool isGrounded = true;
Vector3 velocity;
public float gravityForce = -19.62f;
float _slopeAngle;
//Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical);
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
//jumping
if (isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
velocity.y = Mathf.Sqrt(jumpForce * -2f * gravityForce);
}
}
}
void FixedUpdate()
{
if (isGrounded)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = 20f;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = 6f;
}
}
//Ground check
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
//gravity
velocity.y += gravityForce * Time.deltaTime;
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
controller.Move(velocity * Time.deltaTime);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
}
}