- Home /
Clamp player Movement
Here is the deal..., I am trying to make kind of a lane game (almost like subway surfers style) with a flying ship as the player, but i don't want to limit it to the lod boaring 3 lanes, instead... i want to limit it to a straight line across the screen, then as the ship gains speed and the overall game gets faster the line slowly transforms into an arch (sorry for bad spelling if thats the case) somthing that looks like this: obviously this is just an idea at this stage cuz i have literally just thought of it and i haven't done anything like this before so i really dont know how to go about this one.
It's worth mentioning that this is a mobile game, and the controls aren't going to be swiping, instead by tapping on both sides of the screen.
Any help is appreciated.
I am having trouble understanding exactly what you are looking for. Is it a ship that is flying straight out (like pointing forward) but you can move the ship from side to side? Then as they are moving side to side they will follow this line you are talking about which will arc more depending on the speed you are moving forward?
Answer by Acegikmo · Apr 29, 2020 at 08:44 AM
This would be a good use case for bezier curves! You can set the position of the player along the bezier curve, where the t value is the player position, as a value from 0 to 1, where 0 is on the far left, and 1 is on the far right. Then you can reposition the tangent points of the curve as you gain speed to change the shape of it, so, A and D would move up as you go faster
If you want some resources on how they work and how to code them, I wrote about them a while back here!
Another alternative is to use parabolas, though it's a bit more mathematical and less visual!
you can set the player y position with ax², where x is the player x position (assuming 0 is the center in your image) where you change a based on how fast you're going. 0 would be flat, and 0.5 or so would make a soft curve, and 4 would make a sharp curve! You can try it out here if you want to experiment and see how it works!
Thank you, I think that this is just what i need I'll take a look and see, BTW thats a really useful blog post you got there, Nice job!
Answer by FZ_Applications · Apr 29, 2020 at 08:04 AM
Maybe take a look at Mathf.Clamp() see docs
This method returns the value limited to a minimum and maximum.
Thanks but I Already thought of it, Don't think that it wil do it though.
Answer by Cassos · Apr 29, 2020 at 10:41 AM
Sounds very simple but could work very well: You define a heightfactor for the sides and then multiply the current speed with the factor, so it basicly is on the lane but with increasing speed it gets higher on the y-axis.
Then clamp the maxHeight with Mathf.Clamp(value, min, max) and you got what you want. Maybe rotate the ship based on the x postion and done. :P
Greetings,
Max
Your answer
Follow this Question
Related Questions
How to disable A, S, and D keys for third person player movement 1 Answer
Need help with Raycast2D 0 Answers
Zig Zag movement Unity 2 Answers
Can't jump while sprinting 1 Answer