I don’t know what to do, i think the code is working properly(I did set the ground to the layer mask and all)
using System.Collections;
using System.Collections.Generic;
using System.Reflection.Emit;
using Unity.VisualScripting;
using UnityEngine;
public class Movement : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed;
public float groundDrag;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;
[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;
[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 movedirection;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void Update()
{
//ground check
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
SpeedControl();
MyInput();
//handle drag
if(grounded)
rb.linearDamping = groundDrag;
else
rb.linearDamping = 0;
}
private void FixedUpdate()
{
MovePlayer();
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
//when to jump
if(Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
}
private void MovePlayer()
{
//calculate move direction
movedirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
//on ground
if(grounded)
rb.AddForce(movedirection.normalized * moveSpeed * 10f, ForceMode.Force);
//in air
else if(!grounded)
rb.AddForce(movedirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
}
private void SpeedControl()
{
Vector3 flatvel = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
//limit velocity
if(flatvel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatvel.normalized * moveSpeed;
rb.linearVelocity = new Vector3(limitedVel.x, rb.linearVelocity.y, limitedVel.z);
}
}
private void Jump()
{
//reset Y velocity
rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
readyToJump = true;
}
}
so i made these changes but the player still does not jump if i press space. Do you know if something else is wrong?
Try
Debug.Log($"Condition to jump — {Input.GetKey(jumpKey) && readyToJump && grounded}");
right before the very condition in MyInput(), by the way, GetKey returns method true all time when the button is pressed, as far as I now… It is not good for if-condition, more like it made for while-loops. So maybe better to use GetKeyDown.
Keep also in mind, that you char will be jump before it can move, due to FixedUpdate runes after the Update method. Me too sleepy right now to say more…
Yes so i did the debug thing code and it says that jumping is false when i am touching the ground, how can i fix this?
I do not know… >_< But maybe, the good idea will be to understand which of arguments in boolean equation is ‘false’.
Instead of use the full equation, we should use separate arguments.
Debug.Log($"Key to jump — {Input.GetKey(jumpKey)}");
Debug.Log($"Readiness to jump — {readyToJump}");
Debug.Log($"Groundness to jump — {grounded}");
Try it when character stands on the ground and look on the console (Ctrl + Shift + C).
Aight so. readiness to jump is true. Key to jump and groundness to jump is false
sday2
February 6, 2025, 12:25am
11
I’d check to make sure your raycast is going where you want, just to make sure.
Debug.DrawRay(transform.position, Vector3.down* (playerHeight * 0.5f + 0.2f),Color.green)
And for Key to jump returning false, just do a simple getinput in update with it, but have it return a debug log.
where should i put the code for the ray? also i put the reset jump function in the update, is that correct?
using System.Collections;
using System.Collections.Generic;
using System.Reflection.Emit;
using Unity.VisualScripting;
using UnityEngine;
public class Movement : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed;
public float groundDrag;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;
[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;
[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 movedirection;
Rigidbody rb;
void Start()
{
readyToJump = true;
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void Update()
{
//ground check
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
SpeedControl();
MyInput();
//handle drag
if(grounded)
rb.linearDamping = groundDrag;
else
rb.linearDamping = 0;
ResetJump();
}
private void FixedUpdate()
{
MovePlayer();
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
Debug.Log($"Key to jump — {Input.GetKey(jumpKey)}");
Debug.Log($"Readiness to jump — {readyToJump}");
Debug.Log($"Groundness to jump — {grounded}");
//when to jump
if(Input.GetKeyDown(jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
}
private void MovePlayer()
{
//calculate move direction
movedirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
//on ground
if(grounded)
rb.AddForce(movedirection.normalized * moveSpeed * 10f, ForceMode.Force);
else
rb.AddForce(movedirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
}
private void SpeedControl()
{
Vector3 flatvel = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
//limit velocity
if(flatvel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatvel.normalized * moveSpeed;
rb.linearVelocity = new Vector3(limitedVel.x, rb.linearVelocity.y, limitedVel.z);
}
}
private void Jump()
{
//reset Y velocity
rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
readyToJump = true;
}
}
So i put the jump into void update and now the only thing that is wrong is the is that groundness to jump is false when i press or don’t press jump
I FIXED IT OMG YIPPIE. It was just the raycast being too short it seems, cause i made the raycast longer and it worked. im so dumb lol
sday2
February 12, 2025, 12:15am
15
Good job! Just for future reference, you can put the debug pretty much anywhere, but in update would probably be best so you can see it at all times.