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
1
Question by Flemeggo · Aug 23, 2021 at 07:56 PM · beginnerteleport

trouble with teleportation,trying to teleport the player to a co ordinate or an empty game object

perquisite: sorry this is brief and doesn't give a whole lot of explanation, i wrote a more in depth explanation but after signing in it deleted it :(

ok so I am very much a beginner, literally my first game, 2D top down, I've got the movement working, and the collision with walls and stuff, I'm struggling with the teleportation after the player dies (as respawning) the script that I'm doing it through is done through the object that kills the player, (a small red box that kills on collision trigger) I imagine this is an overly complex way to do it but i did it for a reason that has gotten lost in confusion, I need help lmao, preferable through discord, Flemeggo#0991. 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
0

Answer by Cryno1000 · Aug 23, 2021 at 09:07 PM

What you could do is create a function on the player that is called whenever the player is supposed to die (so you can call the function from anywhere and don't have to duplicate code for different things that kill the player). If you would like you could just reload the scene when the player dies (it would reset everything as if you had just loaded it for the first time). Depending on the type of game you're making, this is probably the best solution unless you want to keep certain variables or objects after the player dies. That would look something like this:

    using UnityEngine.SceneManagement;
         public void Die()
         {
               SceneManager.LoadScene(SceneManager.GetActiveScene);
         }

If you wanted everything except for the player to remain how it was (like if you wanted enemies to stay dead or whatever), you could reset just the player back to their original position or to a GameObject you've created. So if you were doing it with a coordinate, it would look something like this:

 //We use a variable instead of defining it in the script so we can edit the position from the inspector.
 public Vector3 respawnPosition = new Vector3(0,0,0)
 
 public void Die()
 {
      transform.position = respawnPosition;
 }

Then if we were doing it to a GameObject, we would want to be able to get a reference to it somehow. So for this example, give the respawn position GameObject a tag (for this example, I'll use "RespawnPosition"). Then, the script would look something like this:

 public void Die(
 {
      transform.position = GameObject.FindGameObjectWithTag("RespawnPosition").transform.position;
 
      //Or...
      Vector3 respawnPosition = GameObject.FindGameObjectWithTag("RespawnPosition").transform.position;
      transform.position = respawnPosition;
 }

Then on whatever kills the player, you just need to call that function. So for example on your red box, you would do something like this:

 void OnCollisionEnter2D(Collision2D col)
 {
      col.GetComponent<ScriptName>().Die();
 }


This will call Die(), and whatever option you chose to do will be done.

Let me know if you have any other questions and good luck on your game dev journey!

Comment
Add comment · Show 2 · 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 Fauxvoid · Jan 03 at 12:15 PM 0
Share

This is really good, however there might be a problem. " 'Collision2D' does not contain a definition for 'GetComponent' " (col.GetComponent doesn't work) Am I doing something wrong or did you make a typo?

avatar image Hellium Fauxvoid · Jan 03 at 01:41 PM 0
Share

col.collider.GetComponent

Or

col.gameObject.GetComponent

Or

col.transform.GetComponent

Or

col.rigidbody.GetComponent

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

141 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

Related Questions

Script to teleport player (Beginner here) 0 Answers

Teleport Player in 2D game? 4 Answers

Player slows down every time they are movies? 0 Answers

Unity jumping sound, help. 1 Answer

How to upload a game to the internet 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