So basically I have a rectangle with a Dynamic Rigidbody2D and BoxCollider2D attached to it and I can make it jump into the air by pressing the space bar.
I’m trying to apply extra force to the game object to travel back down to the ground faster than it comes up during the jump.
I’ve written the following code:
if(_rb.velocity.y < 0){
_rb.velocity = new Vector2(_rb.velocity.x,_rb.velocity.y * 1.15f);
}
This code seems to work but I’m getting this weird glitchy effect where the rectangle sort of goes through the ground for a split second before it rests flush with the ground. Here is a video of what I’m talking about here:
I don’t understand why this is happening if both the ground and the rectangle have BoxCollider2D’s attached to them and the rectangle also has a Dynamic Rigidbody2D attached to it?
You should understand that the only way the physics system can solve collisions is by modifying the velocity; something which you’re actively trying to defeat by amplifying the downward velocity so I’d say that the solver did a good job. Also, you’re using discrete collision detection which allows overlaps which it then solves whereas continuous takes more CPU time but endeavours to stop overlaps in the first place. Using discrete you’ll step into the ground (by the look of it, very fast) then the solver tries to solve the overlap but you’ll continue to try to make it go faster downwards. Luckily it defeats that code but only after a small period of time.
Why not modulate gravity scale rather than stomping on velocity?
You can see the effect of setting a higher gravity scale when moving downward and a lower gravity scale when moving upward here:
Okay so how would I go about modulating the gravity scale in code in order for the rectangle to descend faster than it ascends during a jump? I’ve tried writing the following but it doesn’t seem to work:
There’s no point in doing that in Update as it’s only dealt with during the simulation during FixedUpdate so place it there (unless you’re running physics per-frame of course).
Not sure what this is about as it’d have no effect.
Set your Rigidbody2D to use Continuous collision detection mode too.
Okay so I’ve followed what you’ve said to me and I’ve written the following code below that will change the gravity scale based on whether the player is ascending or descending on the Y-axis but I don’t understand how I would be able to force the player to reach a fixed height when it is jumping? Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerThree : MonoBehaviour
{
private Rigidbody2D _rb;
public Transform _groundCheck;
public LayerMask _whatIsGround;
public bool isGrounded;
public float jumpHeight = 7f;
public bool isDescending;
public bool isAscending;
// Start is called before the first frame update
void Start()
{
_rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(_groundCheck.position,0.25f,_whatIsGround);
if(_rb.velocity.y < 0){
isDescending = true;
isAscending = false;
_rb.gravityScale = 20f;
}else if(_rb.velocity.y > 0){
isAscending = true;
isDescending = false;
_rb.gravityScale = 1f;
}else if (_rb.velocity.y == 0){
isDescending = false;
isAscending = false;
}
if(isGrounded){
if(Input.GetKeyDown(KeyCode.Space)){
_rb.velocity = new Vector2(_rb.velocity.x,jumpHeight);
}
}
}
}
And here’s a video of me continuously pressing the space bar so that the player will jump but it never jumps to a fixed height. Sometimes it will jump only a tiny bit and then other times it will jump high: https://www.youtube.com/watch?v=wY7vVXoowGY