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 Boogermann · Aug 20, 2014 at 01:00 AM · transformpositionteleport

Transform Position Not Moving Object

I'm having a problem changing an object position. I have a teleport system that when the player step in he's moved to another position. The code is working fine, but won't change the player object position. Here is my code:

 using UnityEngine;
 using System.Collections;
 public class Teleport : MonoBehaviour {
     public Vector3 pointA;
     public Vector3 pointB;
     private GameObject thePlayer;
     private Vector3 thePlayerPosition;
     private Vector3 thePlayerLastPosition;
     void Start() {
         thePlayer = GameObject.FindGameObjectWithTag("Player");
     }
     void Update () {
         thePlayerPosition = GridMove.playerPosition;
         thePlayerLastPosition = GridMove.playerlastPosition;
     }
     void OnCollisionStay ( Collision other ) {
         print("Player Has Arrived");
         if (thePlayerPosition == pointA) {
             print("Player is on Teleport A at: " + thePlayerPosition);
             if (thePlayerLastPosition == pointB) {
                 return;
             } else {
                 TeleportToPosition( pointB );
                 GridMove.playerlastPosition = pointA;
             }
         }
         if (thePlayerPosition == pointB) {
             print("Player is on Teleport B at: " + thePlayerPosition);
             if (thePlayerLastPosition != pointA) {
                 TeleportToPosition( pointA );
                 GridMove.playerlastPosition = pointB;
             } else {
                 return;
             }
         }
     }
     void TeleportToPosition ( Vector3 thePlayerDestination ) {
         print("Teleport Player to Position: " + thePlayerDestination);
         thePlayer.transform.position = thePlayerDestination;
     }
       
 }

Here is my console output.

 Player Has Arrived
 Player is on Teleport A at: (2.0, 0.0, 2.0)
 Teleport Player to Position: (7.0, 0.0, -2.0)

If i change for example just to test, the player to "this" the block that I step in moves to the position.

 void TeleportToPosition ( Vector3 thePlayerDestination ) {
     print("Teleport Player to Position: " + thePlayerDestination);
     this.transform.position = thePlayerDestination;
 }















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
Best Answer

Answer by smoggach · Aug 20, 2014 at 04:47 AM

You're teleporting the player object to pointB then immediately back to pointA then your system gets out of sync. try else if instead of if in the second case inside OnCollisionStay

    else if (thePlayerPosition == pointB) {

In the first case, after you teleport him to pointB, you update GridMove.playerLastPosition but fail to update thePlayerLastPosition which gets checked immediately after.

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 Boogermann · Aug 21, 2014 at 11:57 PM 0
Share

Thanks. I clean up the code and fixed the movement by transfor$$anonymous$$g the collision object. Works like a charm now. Here is the complete code in someone ever needs it.

 using UnityEngine;
 using System.Collections;
 
 public class Teleport : $$anonymous$$onoBehaviour {
 
     public Vector3 pointA;
     public Vector3 pointB;
     private Vector3 thePlayerPosition;
     private Vector3 thePlayerLastPosition;
     
     void Update () {
         thePlayerPosition = GridPlayerController.playerPosition;
         thePlayerLastPosition = GridPlayerController.playerlastPosition;
     }
 
     void OnCollisionStay ( Collision thePlayerCollision ) {
         if (thePlayerPosition == pointA) {
             if (thePlayerLastPosition != pointB) {
                 TeleportToPosition( thePlayerCollision, pointB, pointA );
             } else {
                 return;
             }
         } else if (thePlayerPosition == pointB) {
             if (thePlayerLastPosition != pointA) {
                 TeleportToPosition( thePlayerCollision, pointA, pointB );
             } else {
                 return;
             }
         }
     }
 
     void TeleportToPosition ( Collision thePlayerCollision, Vector3 thePlayerDestination, Vector3 playerlastPosition ) {
         if(this.GetComponent<AudioSource>() != null) {
             audio.Play();
         }
         thePlayerCollision.transform.position = thePlayerDestination;
         GridPlayerController.playerlastPosition = playerlastPosition;
     }
         
 }
avatar image cshahk · May 30, 2019 at 11:24 PM 0
Share

So, what I figured out is that you have to disable the object you are trying to change position on, set the position and than enable it.

Player.SetActive(false); Player.transform.position = new Vector3(PositionX, PositionY, PositionZ); Player.SetActive(true);

This might be because during gameplay the player or the object you are trying to move is in static mode.

Chirag Shah

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Teleporting Player to a point using box colliders 1 Answer

Transform.Position Help 3 Answers

Moving gameobject relative to resolution 1 Answer

Teleport object to other objects position 2 Answers

Teleportation Loop issue 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