- Home /
Revise one conflicting code line?
So, earlier today I posted a question about how to fix my jumping code. But, I've since narrowed it down to one exact section, the one that's ruining the entire jumping function. Now, the exact line, or pair, that ruins the jumping by making the character shoot up lightning fast, as if teleporting into the air, then slowly floating back down, is this:
Vector3 movement = new Vector2(moveHorizontal, 0);
gameObject.GetComponent<Rigidbody2D>().velocity = movement * playerSpeed;
I can't get rid of these, as they're required not only for movement, but for properly flipping the character sprite when direction is reversed. So, what's conflicting about these lines, and the jumping code? Here is my jumping code, separated:
if (Input.GetButtonDown("Ups"))
{
Jump();
}
Flip(moveHorizontal);
anim.SetFloat("Speed", Mathf.Abs(moveHorizontal));
}
void Jump()
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumppower);
}
And, for reference, here is the entire controller script.
Animator anim;
public float playerSpeed = 10;
public float playerJumppower = 1250;
public bool facingRight;
private Rigidbody2D myRigidbody;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
facingRight = true;
anim = GetComponent<Animator>();
}
void Update ()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis("Street");
anim.SetFloat("Speed", move);
float moveHorizontal = Input.GetAxis("Street");
float moveVertical = Input.GetAxis("Vert");
Vector3 movement = new Vector2(moveHorizontal, 0);
gameObject.GetComponent<Rigidbody2D>().velocity = movement * playerSpeed;
if (Input.GetButtonDown("Ups"))
{
Jump();
}
Flip(moveHorizontal);
anim.SetFloat("Speed", Mathf.Abs(moveHorizontal));
}
void Jump()
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumppower);
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector2 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
The fact that you're setting velocity and adding force is the most obvious. You shouldn't do both. Calculate the required velocity and apply it as a whole, ins$$anonymous$$d of zero the y velocity keep the current y OR only use AddForce
The thing is, both are required for their specific function. If I comment out this:
gameObject.GetComponent().velocity = movement * playerSpeed;
The jumping works perfectly. But, I'm no longer able to move the character. But, if I comment out this ins$$anonymous$$d-
GetComponent().AddForce(Vector2.up * playerJumppower);
...movement works perfectly, and jumping is no longer possible. I've tried revising both lines, but they only seem to work this way, and they conflict with each other.
Answer by FernandoGBR · Oct 01, 2017 at 08:19 AM
public float playerJumppower = 1250;
Isn't that TOO big?
Also try to change the force mode to impulse (that is what you do when you jump)
GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumppower, ForceMode.Impulse);
Well, here's the thing about the 1250 - that number works perfectly when I comment out this line, here:
gameObject.GetComponent().velocity = movement * playerSpeed;
When I do that, the jumping functions exactly how I want it. But, when I do that, the character is no longer able to move left and right. Somehow, though, it still recognizes that movement is supposed to happen, as the character running animations, based on speed, are playing perfectly.
Your answer
Follow this Question
Related Questions
jump with character controller best way? 1 Answer
How do I make this jump function work? 1 Answer
Not able to jump 1 Answer