- Home /
Physics - strange jumps.
Hello, guys! Here's the deal: I've got a 3d world with a 2d character in it (don't let the screenshots fool you, I've enabled 2d for better observation). There is a capsule collider on the character. Here's the code for it's controller:
using UnityEngine;
using System.Collections;
public class JellyNew : MonoBehaviour {
private GameObject child;
public float maxSpeed = 5f; //Declare maximum speed
bool facingRight = true;
//Getting animator component from a child object
public GameObject childWithAnimation; //Declaring input for editor
Animator anim;
void Awake () {
anim = childWithAnimation.GetComponent<Animator> ();
}
//Ground Check
bool onGround = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround; //Declaring input for editor
public float jumpForce = 700f; //Declare jumping force
//bool doubleJump = false;
// Use this for initialization
void Start ()
{
child = GameObject.Find ("SpriteRotate"); //Finding a child which should be rotated
}
// Update is called once per frame
void FixedUpdate ()
{
onGround = Physics.CheckSphere(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", onGround);
//if(onGround)
//doubleJump = false;
//How fast we're moving up or down
anim.SetFloat("vSpeed", rigidbody.velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs(move)); //This is our current vertical speed
rigidbody.velocity = new Vector2 (move * maxSpeed, rigidbody.velocity.y);
if(move > 0 &&!facingRight)
Flip();
else if(move < 0 && facingRight)
Flip();
}
void Update()
{
//If player on the ground and we're hitting 'Jump' button, we become no longer 'on the ground' and we're jumping
if(onGround && Input.GetButtonDown("Jump"))
{
anim.SetBool ("Ground", false);
rigidbody.AddForce (new Vector2(0,jumpForce));
}
}
void Flip() //This hideous, yet effective function rotates the child object with sprite and animation
{
facingRight = !facingRight;
Vector3 childScale = child.transform.localScale;
childScale.x *= -1;
child.transform.localScale = childScale;
}
}
When I stand on a step and jump, I jump just as I intend to - not too high up. That's the correct version. Here's a screen of a normal jump:
THE PROBLEM: When I run towards the stairs and jump off of one step's edge, the character jumps enormously high. You can see how high he jumps on this screen:
What's the problem with this all?
I suspect your grounded check is picking up the vertical edges of the stairs and 'regrounding' your character.
I'm having the same problem. I'll do a little searching around and see if I can get back to you!
Are you sure that's not the actual normal jump distance (given your jumpforce is 700) and that the jump you want is actually the result of the rigidbody and colliders dragging the distance down?
What happens if you jump away from the steps?
I guess no, that's not the normal jump. Because when I jump just standing on the floor or on the steps, he jumps a height of 2 grid squares, but when I jump running past the stair edges, he jumps 4+ squares. I'll make a video of that.
Btw, about 700 jumpforce - I adjusted it in the editor to 400 and the gravity is set to -15.
Answer by drudiverse · Jul 07, 2014 at 04:22 PM
have a global variable that sais "LevelJumpedFrom"... and check the way you send cause effect to physics, the physics jump force musnt get anything else than level jumped from, and you should use rigidbody.velocity, its a read variable, unless you are doing something very core level in your physics... use addforce etc all the jump physics options.
dont use rigidbody.velocity to change things, it's read only
Thank you for your answer, but can you maybe describe more 'for stupid' style? I'm not any good with code yet, and I was doing this coding after a official live training tutorial.
Answer by devcor · Jul 08, 2014 at 05:06 PM
So, guys, here's the video I've made. First few times I jump when the character isn't colliding with any steps at the moment of jumping. And as you can see he jumps almost to the top of the ladder.
But then I start running and jumping at the moment when he hits ladder collider, and you can see that he jumps much higher - he flies over the ladder.