Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by CodeG4M3 · May 15, 2021 at 01:10 PM · listrandom.rangeteleport

Random Teleport Character issue...Help Please.

Hi everyone, I'm starting to take my first steps on unity and I'm trying to make a simple 2d game. I would like to create a teleport within the map and when my character comes into contact with a point he is able to teleport between the various points on the map randomly. As if it were a portal. Now I have managed to insert a list with the points where it can move and with the Random function I can move it. The problem is that the random function also calls the point where my character is already and therefore does not move. This is what I would like to do: If I have three points A B C my character enters point A and can move to points B and C. If he goes to point B he can move to A and C and so on.

Now my character if he enters point A moves randomly to A B C.

How can I go about fixing it? Thank you.

 public class Trasport : MonoBehaviour
 {
 
     public List<GameObject> Portal = new List<GameObject>();
     public GameObject Player;
 
 
 
 
 
     public void changePortal()
     {
         int aCaso = Random.Range(0, Portal.Count);
 
         Player.transform.position = new Vector2(Portal[aCaso].transform.position.x, Portal[aCaso].transform.position.y);
 
         
 
     }
 
 
 }
 

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
0

Answer by fct509 · May 17, 2021 at 09:05 AM

I'm not as familiar with the 2D stuff in Unity, but if this were a 3D, I would brake this into a multi-step process with the use of Triggers (which are just Colliders that have the isTrigger field set to 'true), and I would combine this with a teleport manager. So, when the Colliders get enabled, they will register with the manager, and the manager will give them an id.

Then the teleport process can do something like:

  1. Player enters trigger area, raising an On Trigger Enter message, use it to inform the teleport manager

  2. Teleport manager gets teleport request for player

  3. Teleport manager sees the player isn't registered so it registers that that player is teleporting way from the teleport spot that sent the request

  4. Teleport manager randomly selects a teleport spot until it selects one that isn't the starting location

  5. Teleport manager moves player to the selected teleport location

  6. Old spot raises a On Trigger Exit message, use it to send a message to the teleport manager

  7. Teleport manager gets a player left teleport area, but because the player is registered as initiating a teleport form that location, the teleport manager will just ignore it

  8. New spot raises an On Trigger Enter message and informs the teleport manager

  9. Teleport manager sees that a registered player has suddenly appeared in a teleport area that isn't their starting area, so the teleport manager ignores it.

  10. Player leaves the teleport area raising a On Trigger Exit message, use it to inform the teleport manager

  11. The teleport manager sees that a registered player left a teleport area that they did not start in, so it removes that player's registration, allowing the player to initiate this entire process when they enter any teleport area

By implementing these general steps, you should be able to implement this in a multi-player system. Just remember:

  • Unknown Player enters teleport => Run teleport process

  • Known Player enters any teleport => Do Nothing

  • Known Player exits initial teleport => Do Nothing

  • Known Player exits different teleport => Erase Player, making them Unknown Player

Hope this helps.

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

164 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 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 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 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 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 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 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

random.range to get Random player in list? he best overloaded method match for `System.Collections.Generic.List.this[int]' has some invalid arguments 1 Answer

How do I get random answers? 2 Answers

Generate unique random numbers and sort them. 3 Answers

Loading Assets from a list 0 Answers

How I randomise a list of signals in Unity and remove it from the list once the clip is finished? 0 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