- Home /
Achieving an Asteroids-style screen wrap
Hi all, I've searched through and I can see this has been asked multiple times but none of them had an answer that I could actually make sense of. I'm a C# rookie, and I'm trying to achieve what they've got going on in Unitroids (link: http://forum.unity3d.com/threads/5050-Unitroids). The Scripting Reference has this example for the Mathf.Repeat function:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void Update() {
transform.position = new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y, transform.position.z);
}
}
But this just makes the object slide off to the side and then repeat after a certain amount of time (I think?) and I want it to repeat after hitting the edge of the screen, not time. In the Unitroids thread Eric said that the wrap code they used was:
myTransform.position.x = Mathf.Repeat(myTransform.position.x, aWidth);
myTransform.position.z = Mathf.Repeat(myTransform.position.z, aHeight);
But I can't make that code work, I've really no idea how to even read it!
Any assistance would be really appreciated, and sorry for such a lengthy post!
Thanks!
Answer by Em3rgency · May 18, 2014 at 04:42 PM
That code snippet you have actually solves your problem. Let me help you read it.
myTransform.position.x = Mathf.Repeat(myTransform.position.x, aWidth);
myTransform.position.x reffers to the x coordinate of your object.
aWidth is the width of the play area. So its the size of the area you want to wrap. If you want to wrap everything, just make this something thats slightly larger than your visible area.
Mathf.Repeat() makes sure you dont go over the bounds of your defined area. So if you have Mathf.Repeat(2.1, 2) the function returns 0.1. Meaning, if your asteroid flies outside your size 2.0 play area, it gets moved to the start, ie 0.1.
The code snippet you have is done separately for both x and z (in a top-down view) coordinates. So each time your asteroid goes out of bounds, it pops up at the other end. I hope that clears something up.
Hi Em3rgency, firstly thanks for replying ! But there are a couple of things I'm not sure of. I'm aware this is all pretty basic stuff but I'm new to Unity and program$$anonymous$$g (having had an 'artist' background in UD$$anonymous$$) so please bear with me D:
Firstly I don't anything that's actually named myTransform, so should I put some reference to the player GameObject in there ins$$anonymous$$d, like 'Player'? It's just that when I did put 'Player' or 'Transform' in there I got an error :/ Also, how do I define the numerical values in aWidth, should I just declare an integer value of 689 at the beginning of my script?
Thanks very much again for your assistance, I really appreciate it!
Sorry for the super late reply, just noticed your second question.
Attach the script to your spaceship or whatever and then you can access "myTransform" by simply writing "transform". Unity will understand that you're accessing the ships transform, because the script is attached to it. The reason it says "myTransform" is because sometimes you want to modify other objects, that the script is not attached to, so you need to get their transform first, like you said.
And yes, for the width and height you can just type in your resolution (but height needs to be negative, if I'm not mixing up my screen space again).
Did you ever get this working @shrapnel92? I'm trying to achieve the same result but everything I have seen is so over complicated. I tried this code and couldn't make anything of it. I understand it but not how it works. I did this to try and make it work with C#.
public float aWidth = 80;
public float aHeight = 60;
Void Update()
{
transform.position.x = $$anonymous$$athf.Repeat (transform.position.x, aWidth);
transform.position.z = $$anonymous$$athf.Repeat (transform.position.z, aHeight);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
gameObject destroying itself 1 Answer
Flip over an object (smooth transition) 3 Answers