(i created this post again cuz i forget to add tags in previous one)
Hello, i have a issue in my 2D game when my character is on platform which is moving up and down jump is very low when platform go up and very high when platform go down
when character is on platform, platform becomes parent of that character gameobject
private void OnCollisionEnter2D(Collision2D colPlatform) //script attached to character gameobject
{if (colPlatform.collider.tag == "VerticalPlatform") gameObject.transform.parent = colPlatform.transform;}
void jumpFromPlatformButton() //script attached to character gameobject
{
gameObject.transform.parent = characterParent;
RB.velocity = new Vector2(jumpLenght, jumpHeight) * Time.fixedDeltaTime;
}
Thanks in advance
That’s to be expected. You’re setting a fixed jump velocity for the player and the relative velocity between the platform and the player is what counts. When the platform is going up, the relative velocity is playerVel - platformVel
, when it’s moving down it’s playerVel + platformVel
. How big the difference is depends on how fast the platform is relative to the player’s jump velocity but it’s going to be 2 * platformVel
.
If you want to keep the relative velocity the same, you need to add the jump velocity to the platform’s velocity. How exactly you do that depends on how your platform is set up and how general you want your implementation to be. A solid but limited way would be to get the platform’s script in the collision enter callback and read from it how fast the platform is moving. You could also try to read it from the rigidbody or use the player’s current velocity but that will likely not be as stable.
You’re also using Time.fixedDeltaTime
wrong. Delta time is used to spread something over multiple frames, it turns a unit
into units per second
, for when you want to move something at a constant speed or accelerate at a fixed rate. You don’t use it when something is applied in a single instant, like in this case, when overriding a rigidbody’s velocity. This corresponds to the physics ForceModes, where Force
and Acceleration
are intended to be applied over multiple frames and internally multiply by delta time, whereas Impulse
and VelocityChange
are for single instants and do not multiply by delta time. I’d also recommend to just use Time.deltaTime
, which automatically returns fixedDeltaTime
inside physics updates. This makes it less likely to use the wrong delta time by accident.