Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Serge144 · Mar 13, 2020 at 04:20 PM · vector3transform.positiontranslateteleport

How to teleport gameobject (instantly change transform.position)

I'm trying to do a "quick restart" of a level kind of thing. So in order to do this i'm just saving the initial position of the Player on the Start method as such:

 initialPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);

Then on Update, everytime the player goes to an area it shouldn't, then it should go back to the initial position.

 if (transform.position.y <= 2f){
  gameObject.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
 }

This does absolutely nothing.. and all other answers say to do exactly this... Any help? Thanks in advance.

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

3 Replies

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

Answer by Tripleganger · Mar 13, 2020 at 05:41 PM

Hey @Serge114! First, instead of gameObject.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z); you can write gameObject.transform.position = initialPosition;.

Then, regarding the main issue:

  1. Are you sure your object's position is ever going to be <= 2f?

  2. Remember that transform.position refers to the object the script is attached to; if this script is not attached to your player, it will not work.

  3. Have you actually assigned the script to your player?

  4. Is if (transform.position.y <= 2f){ gameObject.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z); } nested inside another if statement that is never valid?

Hope this helps.

Comment
Add comment · Show 9 · 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 Serge144 · Mar 13, 2020 at 06:34 PM 0
Share
  1. Yes, because i'm printing a Debug.Log when that happens

  2. The script is attached to a GameObject called Player, which has 6 components, Transform, Cube ($$anonymous$$esh Filter), $$anonymous$$esh Rendered, Box Collider, PlayerController, CharacterController

  3. Yes the script is attached (PlayerController.cs)

  4. It is not, it's in the Update's scope.

avatar image tormentoarmagedoom Serge144 · Mar 13, 2020 at 07:01 PM 0
Share

Hello. Post then all your Update code where you do the Debug.Log. When do you store the initialPosition vector? Also post a screenshot of the Unity Editor Inspector of the Player GameObject.

avatar image Serge144 tormentoarmagedoom · Mar 13, 2020 at 07:30 PM 0
Share

The initalPosition vector is stored on general scope and initialized on Start method.

 void Update()
     {
         bool touchedJoystick = joystick.Horizontal != 0 || joystick.Vertical != 0;
         if (!started$$anonymous$$oving && touchedJoystick) {
             game$$anonymous$$anager.StartedLevel();
             started$$anonymous$$oving = true;
         }
       
         if(transform.position.y <= 2f){
             Debug.Log("Reset");
             gameObject.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
         }
         
         float horizontal = joystick.Horizontal;
         float vertical = joystick.Vertical;
         
         Vector3 input = new Vector3(horizontal, 0f, vertical);
         Vector3 inputV2 = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * input;
         controller.Simple$$anonymous$$ove(inputV2 * 9);
         if(inputV2 != Vector3.zero)
             transform.rotation = Quaternion.LookRotation(inputV2, Vector3.up);
         
         Vector3 gridPos = ConvertWordCor2TerrCor(transform.position);
         
         int x = (int)gridPos.x;
         int y = (int)gridPos.z;
 
         coverCircleArea(x, y, 15);
     }

alt text

avatar image Tripleganger · Mar 13, 2020 at 10:34 PM 2
Share

I have tried to reproduce the whole thing and... You are right, it does not work. So I made it in my own terms, which are only slightly different from yours.

  1. Declare`private Vector3 v3_Original;` as a member (variable).

  2. In Update (Or even better, LateUpdate) write` if (gameObject.transform.position.y <= 2f) { gameObject.transform.position = v3_Original; }`. Attach this to your player and it will work, as I have just tested. Here's the full script:

      private Vector3 v3_Original;
     
         // Start is called before the first frame update
         void Start()
         {
             v3_Original = gameObject.transform.position;
             Debug.Log(v3_Original);
         }
     
         // Update is called once per frame
         void Update()
         {
             if (gameObject.transform.position.y <= 2f)
             {
                 Debug.Log(v3_Original);
                 gameObject.transform.position = v3_Original;
             }
         }
    
    
    
avatar image Serge144 Tripleganger · Mar 13, 2020 at 10:49 PM 0
Share

oh it works if i put it on LateUpdate lol, if i put the same piece of code in Update it does not work. Thanks! :)

avatar image joemane22 Serge144 · Mar 13, 2020 at 10:52 PM 0
Share

A lot of times this is due to the physics engine not being in sync with the changes. You can calls Physics.SyncTransgorms after the position change to make it work in Update

https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html

Show more comments
Show more comments
avatar image
0

Answer by aoun111 · Mar 14, 2020 at 06:45 AM

If you use the FPS Controller, disable that script and the Character controller before teleporting, and enable them again after teleporting

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 Supphakon · Apr 13, 2020 at 01:10 PM

 GameObject.tranfrom.position = new Vector 3 (psc x ,pos y , pos y)

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

156 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

Related Questions

Getting Vector3 in between two given Vector3 after given Distance. 2 Answers

Player slows down every time they are movies? 0 Answers

smoothly change position of an Object 1 Answer

Translation of an Object 0 Answers

Simple script to move object doesn't work 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