Simple Jumping Problem with raycast,Simple Jumping Problem
Hey i don't know whats wrong with my code , sometimes its jumps once only , and sometimes it jumps only when i press the movement keys like "q" or "d" , if there is any improvement that i can do that code please tell me here is my code
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody rb;
void FixedUpdate()
{
if (Input.GetKey("z"))
{
rb.AddForce(0, 0, 2000 * Time.deltaTime);
}
if (Input.GetKey("d"))
{
rb.AddForce(2000 * Time.deltaTime, 0, 0);
}
if (Input.GetKey("q"))
{
rb.AddForce(-2000 * Time.deltaTime, 0, 0);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, -2000 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space) && isGrounded())
{
rb.AddForce(0, 20000 * Time.deltaTime, 0);
}
}
bool isGrounded ()
{
return Physics.Raycast(transform.position, Vector3.down, 0.5f);
}
}
,Hey i don't know whats wrong with script , it does the jump sometimes and sometimes only when i press other keys like "q" "d" here is my script
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody rb;
void FixedUpdate()
{
if (Input.GetKey("z"))
{
rb.AddForce(0, 0, 2000 * Time.deltaTime);
}
if (Input.GetKey("d"))
{
rb.AddForce(2000 * Time.deltaTime, 0, 0);
}
if (Input.GetKey("q"))
{
rb.AddForce(-2000 * Time.deltaTime, 0, 0);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, -2000 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space) && isGrounded())
{
rb.AddForce(0, 20000 * Time.deltaTime, 0);
}
}
bool isGrounded ()
{
return Physics.Raycast(transform.position, Vector3.down, 0.5f);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612205509im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with jumping script 2 Answers
Jump Using Raycast. 0 Answers
How do you make the character stuck on the ceilling when at that moment the velocity.y is > 0 0 Answers
Need help with jumping on third person movement script. 0 Answers
This is frustrating 2 Answers