- Home /
2D side scroller with loop the loop
For my first unity project, I have built a side scroller/platformer using the unity 2D subset. I would like to introduce a looped track as in the picture below:
The noob question: Must I use 3D objects to implement this loop? Obviously the car and track have to have different Z positions when entering and exiting the loop. Since the 2D colliders seem to ignore Z position come to think of it, perhaps the loop itself can be 3D colliders. Can anyone suggest a simple way of handling this special case in my otherwise 2D game?
Yes, this is good, thanks. I guess only the track segments that cross each other must have their enables toggled.
Yes, that sounds like a good solution! Please give the answer a thumb up if you found it helpfull :)
I'd be happy to... can you convert your comment to an answer? I do not have this option, possibly because I am a newb here.
Answer by Dimling · Oct 22, 2014 at 03:19 PM
Not sure if this is a good way, works at least :P
When you set a texture as Sprite you can define what layer to be at. You can for instance have the front track to be at layer 2 while the lower part of the loop is layer 1. This will then place the layer one behind layer two as displayed on your picture. The problem now will of course be the collision. But with help of some simple code you can for instance remove and add collision based on the current position of your cart.
Code to remove or add collision:
collider.enabled = true;
Hope this helps
I implemented this with one catch... maybe it is a separate problem. I start with a disabled collider2d on the "exit ramp". Then when the vehicle leaves the "entrance ramp" I used your suggestion and enabled the collider on the exit-ramp. I also tweak with the vehicle's gravity a bit to make sure it stays relatively stuck to the top of the loop.
But my problem is, I would also like a Collider2d w/Trigger+Script on the exit ramp to un-tweak the vehicle's gravityScale. For now, I am simply putting that trigger on the section of platform after the exit ramp.
I am sorry, not sure if I understand the question. Why can't you add this to the exit ramp?
$$anonymous$$aybe a separate problem... but when a collider is setup as a trigger, it no longer uses physics (objects pass through it). If I add a rigidBody2d, well now I need to disable and enable it (which I am not sure I can).
Sorry for late answer!
If the cart is touching the ramp, physically, you might use OnCollision2D ins$$anonymous$$d of the trigger functionality.
Unity Documentation: OnCollision
Then you can for instance write like this:
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Exit Ramp")
UnTweak();
}
I guess this is noob stuff... I was triggering collisions from the perspective of the terrain, but doing it from the Player (vehicle) is best for things that affect the vehicle. I'd give you another thumbs up if I could, thanks. :)
Answer by Tepei · Oct 22, 2014 at 06:32 PM
You could use this
http://docs.unity3d.com/ScriptReference/Physics2D.IgnoreCollision.html
The tricks is wheen a ignore collision is done; then you want back the physics to works between the object, you have to deactivate the object and reactivate it in the same frame; that reinitialise the collision..
Your answer
Follow this Question
Related Questions
2D Autoscroller Glitches 0 Answers
In sequence position game objects 1 Answer
Created Material Shows Dark Sprite 1 Answer
Hiding specific areas of unity2d layers. 0 Answers