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 SirBoboHobo · Mar 15, 2015 at 03:33 PM · androidscreenteleport

"teleport" player depend on screen width

I want my sides to be "connected" if the player exits from the right side of the screen he will get out of the left, and the opposite, same as in Doodle Jump

Comment
Add comment · Show 1
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 savlon · Mar 15, 2015 at 06:18 PM 0
Share

Screen wrap https://www.youtube.com/watch?v=mq4BlZLRe$$anonymous$$k

3 Replies

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

Answer by siaran · Mar 15, 2015 at 06:02 PM

Tranform your player's world position to screen position (Camera.main.WorldToScreenPoint), then check his X-coordinate in screen space. If it's less than 0 he's to the left of your screen, if it's more then Screen.width hes at off screen at the right, so you need to teleport him.

Determining where to teleport him to is essentially the same but in reverse: determine the point on the screen you want to have him appear, then transform that to world coordinates (Camera.main.ScreenToWorldPoint), then teleport your player to that position.

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 SirBoboHobo · Mar 15, 2015 at 07:18 PM 0
Share

As someone new to Unity I don't understand this too much, could you show me a sample code please?

avatar image siaran · Mar 15, 2015 at 08:18 PM 0
Share

Sure. In a script atttached to your player:

 void Update(){
   //get the screen position
   Vector3 scrPos = Camera.main.WorldToScreenPoint(transform.position);
   //Check if we are too far left
   if(scrPos.x < 0) TeleportRight(scrPos);
   //check if we are too far right
   if(scrPos.x > Screen.width) TeleportLeft(scrPos);
 }

 //TeleportRight moves character from it's current x position to x position 10 pixels left from the right edge of the screen   
  void TeleportRight(Vector3 scrPos){
 
   //this is the position on the screen we want to move the character too, we only want to change it's x-coordinate
 Vector3 goalScrPos = new Vector3(Screen.width-10, scrPos.y, scrPos.z);
 
  //translate goal screen position to world position
  Vector3 targetWorldPos = Camera.main.ScreenToWorldPoint(goalScrPos);
  
  //move player
  transform.position = targetWorldPos;
 
 }


Does that help?

avatar image SirBoboHobo · Mar 15, 2015 at 08:31 PM 0
Share

Great! Thanks a lot, great code, great explanations.

avatar image
0

Answer by zRevenger · Mar 15, 2015 at 11:15 PM

try to do it in 2 javascripts.the first script:

 var leftSide : Transform;
 
 var rightSideScript : NameOfTheScript;
 
 var character : GameObject;
 
 function OnTriggerEnter()
 
 {
 
         if(character.transform.position == leftSide.position)
 
         {
 
             character.transform.position = rightSideScript.rightSide.position;
   
             Debug.Log("Side Changed");
 
         }
 
 }

the second script:

 var rightSide : Transform;
 
 var leftSideScript : NameOfTheScript;
 
 function OnTriggerEnter()
 
 {
 
         if(leftSideScript.character.transform.position == rightSide.position)
 
         {
 
               leftSideScript.character.transform.position = leftSideScript.leftSide.position;
 
               Debug.Log("Side Changed")
 
         }
 
 }

now you need to insert the right side script in a gameobject on the right side of the script and the same thing for the left side. to the gameobjects add a box collider and set it as a trigger. on the left side script add the character gameobject and the right side gameobject and add himself. to the right add the left side gameobject and himself. now it showld work.

Comment
Add comment · 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
0

Answer by AlexV03 · Mar 01, 2021 at 02:02 PM

I am using this script and how can i change it so if the player goes to the right he teleport to the left. My idea was copy and past the TeleportRight() and rename it to TeleportLeft() and make the transform.position = targetWorldPos; to transform.position = - targetWorldPos that works but the players y axis isn't working so if my player has a y value of y-3 he teleports to y3 how to fix that?

Comment
Add comment · 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

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

24 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 avatar image avatar image avatar image

Related Questions

Touch Hd screen drag problems 0 Answers

How to scale my camera to fit all android and iOS screen sizes 1 Answer

How to play Unity on Android's background Home Screen? 1 Answer

[Android] How to dim screen without locking device? 3 Answers

What is the update for iPhoneSettings.screenCanDarken 2 Answers


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