- Home /
Problem with simple jumping.
Hello, I've been stuck on this problem so much and I can't find a solution. So I have a ball that moves around completely fine but jumping isn't working it supposed to. It jumps fine EXCEPT on FLAT surface. I find this very odd ( I'm losing my mind can't find a way to make it work). I've put up some images for you to see. images
This is my whole player script.
public class BasicMovement : MonoBehaviour {
public float speed;
public float desiredMaxSpeed;
public float jumpHeight;
public float airHover;
private Rigidbody rb;
private bool grounded;
private bool isjumping;
private bool isMoving;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void OnCollisionStay(Collision col)
{
if (col.gameObject.tag == "Walls")
grounded = false;
else
grounded = true;
isMoving = true;
}
void OnCollisionExit(Collision col)
{
grounded = false;
isMoving = false;
}
void Update()
{
if(Input.GetButton("Jump") && grounded == true)
{
isjumping = true;
}
}
void FixedUpdate()
{
if (isMoving == true)
{
Movement(speed);
manipulateSpeed(desiredMaxSpeed);
}
Movement(airHover);//ball moves a bit while in air.
if (isjumping == true)
{
BallJumping();
}
}
void manipulateSpeed(float maxSpeed) //this change the maxSpeed of the ball.
{
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
}
public void Movement(float currentSpeed)
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal ,0, moveVertical )*currentSpeed;
rb.AddForce(movement * currentSpeed * Time.deltaTime);
}
void BallJumping()
{
rb.velocity = rb.velocity+Vector3.up * jumpHeight;
isjumping = false;
}
}Note: I increase my gravity to fall faster.
Can someone test this. I don't know why this is happening.
Answer by foxtrot117 · Mar 24, 2018 at 01:22 AM
Your problem is probably the OnCollisionStay(). First of all be sure that ground is not tagged as "walls". Also mind that if player comes in contact with walls while he is suppose to be grounded, his state will change to "not grounded" because OnCollisionStay() is triggered(it triggers after OnCollisionEnter() which is almost immediately). Then your player will need to get separated from the ground and then contact it again so grounded variable is reset to true.
I would suggest to change the method you use for detection and go for Physics.Raycast() which is much more commonly used. This because a lot of issues can be avoided, many of which cannot be noticed or said. Just Raycast() a line to -Vector3.up and check if it collides with the ground.
-Hope i helped
Sadly, using Raycast didn't solve it. Physics.Raycast(transform.position, -Vector3.up, dist + 0.01f); Jump still slows on flat surface and one thing that I found out is that changing GetButton to GetButtonDown is making the ball completely unable to jump high in any ground.
Aha! If changing the getbutton to getbuttondown makes it not able to jump high on any ground, that means your force adding is being called more than once on uneven ground. The problem isn't that you are jumping too low on flat ground, it's that you are jumping too high on uneven ground. Change it to GetButttonDown as you said, and just increase jump height. Then it should be consistently high.
Your answer
Follow this Question
Related Questions
i want to move a ball on z axis and jump on y axis , 2 Answers
Ball rolling across boxes randomly jumping 1 Answer
I Have A Jump Script, But I need to Edit it To Where The Player Is Only Limited To One Jump 2 Answers
Ball Rolling Game Help 0 Answers
How to stop multiple mid-air jumps on a gameObject? 4 Answers