• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ntbinquiries · Feb 20 at 12:20 AM · 2dphysics2dsidescrollerslidingslopes

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ntbinquiries · Feb 21 at 12:15 AM 0
Share

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: alt text

d.png (34.1 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

302 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D - Detect Slopes x Walls 1 Answer

Player stops moving upon collision 0 Answers

Remove Drag/Friction 1 Answer

Creating a 2D jelly effect without physics 1 Answer

2D Animation does not start 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges