- Home /
Sliding along ramps in a "wave-like" side-scroller
Currently recreating the wave/dart mode from Geometry Dash like in this video at 0:13 in Unity.
Simply put, the player navigates through ramps and slopes by holding down the mouse button to go up at a 45 degree angle. If they are not holding, they go down at a 45 degree angle.
The problem that I have is that I want the player to have the ability to slide up these ramps too without having to press anything. For example: Image (the red square is the player, the white line is his trail, and the pink arrow is how he should be sliding upwards)
The player should be able to just slide up the ramp without having to hold down the button.
After many failed attempts at trying to set the player's rigidbody velocity to go upwards along with the ramp via OnCollisionEnter2D, I am still trying to find a way to implement this sliding without the player colliding with the ramp and therefore offsetting it by a few pixels.
Player code (without any sliding): Player.cs
Player's rigidbody component: rb2d
Without any sliding implemented, the player just simply slams his head into the ramp and doesn't go up, and gets left behind by the scrolling camera. Video: Video
Any help would be appreciated, cheers!
Note: I'm able to attach the project files if need be!
Answer by Rafit345 · Feb 20 at 02:34 AM
There are a couple alternatives here. The simple workaround might be to uncheck freeze rotation z and let physics do its thing. Alternatively, you could set the speed of the block so it goes over the ramp. First you make the ramp's collider a trigger. If ramps have different slopes, you could either make the player object multiply its speed by the ramp's angle so it follows a correct trajectory. Here is an example
public Vector2 angleToVector(float angleToUse)
{
// this is a function I created myself to use in various projects :)
angleToUse *= Mathf.Deg2Rad;
return new Vector2(Mathf.Cos(angleToUse), Mathf.Sin(angleToUse));
}
OnTriggerEnter2D(Collider2D col)
{
float angle = col.gameObject.GetComponent<RampScript>().angle;
// Get the angle of the ramp in some way
rb2d.velocity = angleToVector(angle) * speed;
}
Or, a better way to do this might be to get the position of the upper and lower points of the ramp, and when OnTriggerEnter2D is called, store them both as Vector2s and use Mathf.Lerp to Lerp the position of the playerobject between the lower point Vector2 and upper point Vector2.
Either way, you gotta find a way to obtain the ramp's angle or upper and lower points.
I actually managed to get it working using a similar method to yours! I found the angle of the slope (as a trigger) we are touching using trigonometry (angle = arctan(bounds.size.y / bounds.size.x)) and set the vertical velocity to the tangent of that angle then multiplied by speed. I had tried using triggers before but I guess I gave up too early without actually thinking about using any maths haha Still needs some work for sliding down slopes when going up into them but I assume it is not too hard. Code if anyone is interested:
Your answer
Follow this Question
Related Questions
2D - Detect Slopes x Walls 1 Answer
(2D) MovePosition slows falling 1 Answer
Get information on ignored collision 1 Answer
Unity 2D - Rigidbody2D Velocity sudden freeze 0 Answers
Horizontal gravity on One gameobject 2 Answers