What should I do to improve this basic movement script. I’m going for gang beasts style movement. Also I’m using the new input system.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour {
// Variables
// Input Master
public InputMaster controls;
// Movement Variables
float moveVertical;
float moveHorizontal;
float moveUp;
public float speed = 5f;
public float jumpForce = 7f;
// Misc
private Rigidbody rb;
public SphereCollider col;
public LayerMask groundLayer;
Transform target;
Vector2 movementInput;
void Awake()
{
// Input
controls = new InputMaster();
controls.Movement.Movement.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
controls.Movement.Jump.performed += ctx => Jump();
}
void Start()
{
// Accessing Components
rb = GetComponent<Rigidbody>();
col = GetComponent<SphereCollider>();
}
void Update()
{
// Taking Input
moveVertical = movementInput.y;
moveHorizontal = movementInput.x;
// Moving Player
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
if (movement != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
}
transform.Translate(movement * speed * Time.deltaTime, Space.World);
}
void Jump()
{
// Jumping
if (IsGrounded())
{
rb.velocity = new Vector3(0, jumpForce, 0);
}
}
private bool IsGrounded()
{
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x,
col.bounds.min.y, col.bounds.center.z), col.radius * .9f, groundLayer);
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}