so, I tried following a tutorial on how to make your character jump, but for some reason, I press the space key and nothing happens, I’m pretty new to gamedev, so it 100% can be my fault, but I just cant see what I’m doing wrong
here is the full movement code:
using System;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 20f; // Max speed
public float acceleration = 5f; // Rate at which speed increases
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
public KeyCode jumpKey = KeyCode.Space;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;
Rigidbody rb;
float currentSpeed = 0f; // Current speed, starts at 0
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
UnityEngine.Cursor.visible = false;
readyToJump = true; // Initialize readyToJump to true
}
void Update()
{
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
MyInput(); // Add this line to call the MyInput method
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
// Calculate target angle based on movement direction
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
// Gradually increase speed based on acceleration
currentSpeed = Mathf.MoveTowards(currentSpeed, speed, acceleration * Time.deltaTime);
// Move the player in the direction of the camera
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * currentSpeed * Time.deltaTime);
}
else
{
// If no input, stop moving smoothly
currentSpeed = Mathf.MoveTowards(currentSpeed, 0f, acceleration * Time.deltaTime);
}
}
private void Jump()
{
rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z); // Reset y velocity before applying the jump force
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
readyToJump = true;
}
private void MyInput()
{
if (Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
}
}
any help is appreciated, thanks:]