- Home /
Transform position?
Hello. I want to make a space shooter like asteroids (where the screen wraps around, so if the player hits an edge, they will go to another place). When I am using transform.position, how can I use the player's current position for two values and have the other one be incorporated into a simple math function? So it would be something like this
transform.position = Vector3(Current X, Current Y, Current Z-30);
Thanks.
Did you read my answer and was it useable or do you still need something to make this work? Please answer by checking the correct answer or provide us with further details.
Answer by BerggreenDK · Dec 05, 2010 at 08:56 AM
I would do a check for objects where I use the "size of the screen" as margins. These can be found at application start.
Look here: http://unity3d.com/support/documentation/ScriptReference/Screen.html
Now you have the MAX pos for both directions. Then make a simple check on the coords of each object. Are we outside? if we are, then add/subtract the size from the objects pos.
Depending on your "center" position of your camera, you might have to ajust the values a bit, but I expect you to place 0,0,0 in center of screen for this example.
So I globally initiate the width and height and divide by 2 so I have the center position.
void function CheckBorder(Transform o) { if (o.position.x < xHalfScreenSize) { o.position.x += xScreenSize } else // else is important, otherwise you would switch back and forth in same check { if (o.position.x > xHalfScreenSize) { o.position.x -= xScreenSize } }
if (o.position.y < yHalfScreenSize) { o.position.y += yScreenSize }
else // else is important, otherwise you would switch back and forth in same check
{
if (o.position.y > yHalfScreenSize) { o.position.y -= yScreenSize }
}
}
Now you should be able to throw any object into this function.
As you might see, this makes the object "swap" inside visible area, thats not good. So add a little "extra" border to the screen sizes. A good value must be at least the max size of your object. Lets say your largest meteor would be 256 pixels, then just add 256 to the screen sizes, before dividing.
hope this helps.
A fair warning though, when the parameter defined for the measurement is (regardless of the parameter you picked for the job) changed during any part of the game you will get unwanted behaviour unless the exception is caught and dealt with.
+1: Good answer though, don't get me wrong. Just saying don't start messing with screensize ingame or it gets messed up
Yeah, going from embedded to fullscreen and back or perhaps tilting the screen on touchdevices etc. would need some sort of trigger/exception handling, but I didnt want to make the answer too complicated. One problem at a time. :o) Thanks for thoughts though, no offence taken at all!
Answer by FLASHDENMARK · Dec 05, 2010 at 12:02 PM
You could also check for collision like so:
var changePosition = false;
function OnControllerColliderHit (hit : ControllerColliderHit) { if(hit.gameObject.tag == "Typewhateveryouwant") // Make sure the object you collide with has the same tag as written. {
changePosition = true; } } function LateUpdate () { if(changePosition) = true { transform.position = Vector3(?,?,?) // Write the position you wanna "respawn" to. } }
Hope that helpes :)
Your answer
Follow this Question
Related Questions
transform position player teleport 0 Answers
Teleporting Player to a point using box colliders 1 Answer
Making The Hole In A Golf Game 1 Answer
Transform.Position Help 3 Answers