PlayerMovement | Unit 9

PHOTO EMBED

Tue May 17 2022 21:18:43 GMT+0000 (Coordinated Universal Time)

Saved by @BiscuitTinx #c#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PlayerLoop;

public class Movement : MonoBehaviour
{
    [SerializeField] private Transform orientation;

    private float playerHeight = 2;
    
    [Header("Movement")]
    public float speed = 6;
    
    public float groundMultiplier = 7f;
    public float airMultiplier = 0.4f;

    [Header("Sprinting")] 
    public float walkSpeed = 4f;
    public float runSpeed = 6f;
    public float acceleration = 10f;
    
    [Header("Jumping")]
    public float jumpforce = 5f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    [SerializeField] private LayerMask groundLayer;
    private bool isGrounded;

    [Header("Drag")]
    public float groundDrag = 6f;
    public float airDrag = 1f;

    private float horizontal;
    private float vertical;
    
    private Vector3 Input;

    private Vector3 slopeMoveDirection;

    new private Rigidbody rigidbody;

    RaycastHit slopeHit;
    
    private bool OnSlope()
    {
        if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
        {
            if (slopeHit.normal != Vector3.up)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
    }

    private void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundLayer);
        
        ControlDrag();

        slopeMoveDirection = Vector3.ProjectOnPlane(Input, slopeHit.normal);
    }

    public void ReceiveInput (Vector2 _input)
    {
        horizontal = _input.x;
        vertical = _input.y;

        Input = orientation.forward * vertical + orientation.right * horizontal;
    }

    private void FixedUpdate()
    {
        if (isGrounded && !OnSlope())
        {
            rigidbody.AddForce(Input.normalized * speed * groundMultiplier, ForceMode.Acceleration);
        } 
        else if (isGrounded && OnSlope()) 
        {
            rigidbody.AddForce(slopeMoveDirection.normalized * speed * groundMultiplier, ForceMode.Acceleration);
        } 
        else if (!isGrounded)
        {
            rigidbody.AddForce(Input.normalized * speed * airMultiplier, ForceMode.Acceleration);
        }
    }

    public void ControlSpeed(bool sprintInput)
    {
        if (sprintInput && isGrounded)
        {
            speed = Mathf.Lerp(speed, runSpeed, acceleration * Time.deltaTime);
        }
        else
        {
            speed = Mathf.Lerp(speed, walkSpeed, acceleration * Time.deltaTime);
        }
    }

    private void ControlDrag()
    {
        if (isGrounded)
        {
            rigidbody.drag = groundDrag;
        } 
        else
        {
            rigidbody.drag = airDrag;
        }
    }

    public void OnJumpPressed()
    {
        if (isGrounded)
        {
            rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
            rigidbody.AddForce(transform.up * jumpforce, ForceMode.Impulse);
        }
    }
}

content_copyCOPY