Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shrapnel92 · May 18, 2014 at 11:41 AM · c#asteroids

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shrapnel92 · May 19, 2014 at 09:51 AM 0
Share

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!

avatar image Em3rgency · Jun 09, 2014 at 06:08 PM 0
Share

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).

avatar image VAN-D00M · Aug 22, 2014 at 09:34 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Making a bubble level (not a game but work tool) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges