- Home /
How can I program a 'wrap-screen'?
In Asteriods, if you fall off one side, you'll appear on the other directly reversed from your position. How can I program such a thing?
Would it be as simple as doing a raycast backwards until it hits a collider I would place at the boundries of the camera viewport, and then putting the ship at that point?
Thanks, Elliot B.
Top.js
var bottom : GameObject;
function OnTriggerEnter (other : Collider) { other.gameObject.transform.position.x = bottom.transform.position.x; }
Answer by Ryder · Jun 06, 2010 at 08:00 PM
I'm a noob in unity, but have done some programming...
Excuse the simplicity of the suggestion (perhaps you are asking a far deeper question than I am perceiving).
What I have done in the past is simply detect the object passing a boundary. You could probably do it with a simple colision trigger, and simply "warp" the object to the opposite side of the screen by changing its location. It should still retain its motion.
Hope this is not syntactically bogus, but you get the idea:
transform.position.x = -512
Example: when the "ship" is leaving the right side of the play area, at the moment it is still half visible, simply reset the X position to the left side of the screen.
The down side to this is you can't see the object leaving on one side and emerging from the other at the same time... but it's not a serious flaw in my experience.
Of course for many objects, like in asteroids, you would probably have the boundary object detect what object triggered it, and then change the position of the object that touched it... this would work for the asteroids as well as the ship. Even "shots" would warp properly if that is what you wanted.
Ryder
That sounds right. However, I being me, I can't program it right. Do you happen to have an example laying around? Or could you look at the code (I'll post it in my Question) and fix it for me?
Sorry, I don't :) But your code looks reasonable to my untrained eye.
You could change your code to read:
print("collision detected"); //to be sure the routine is firing other.gameObject.transform.position.x = 100; //test jump
to test that the jump is working at all. (just basic debugging)
@Ryder Thanks, that did it. +1 for suggesting obvious solution.
Answer by Benjamin 2 · Mar 06, 2011 at 02:13 AM
Where would you put ("collision detected")? And will it work for 3D?