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 orcinusdev · Oct 01, 2013 at 08:05 AM · transformpositionteleport

transform.position sends object to wrong position

I have a really simple code where a player enters a collider (teleport) and gets moved to another position. When i look in the unity editor, its position is not what I assigned it to (it moves to some place between the right position and the teleport. I'm not using any child objects.

Here is the code assigned to the teleport object.

 void OnTriggerEnter(Collider collider){
     if(collider.gameObject.CompareTag ("Player"))
     {    
            Teleport();
     }
             
     }    
     
     void Teleport()

     {
          PlayerController.pc.myTransform.position = new Vector3(10f, 10f, 0f);
          PlayerController.playerSpeed = 0;
     }
 }

(I got the same results without the teleport method where I just implement the teleport code in the OnTriggerEnter method) I have tried localPosition aswell.

For the player I just have a translation code in the update method and code for rotation based on user input.

Do anyone have a solution for me?

Its landing position can differ widely, it can be close to the right position or close the the teleport, which is very very strange

Thankfull for help

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 hamstar · Oct 01, 2013 at 08:27 AM 0
Share

Can you paste the code where you are assigning the value to PlayerController.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by orcinusdev · Oct 01, 2013 at 08:54 AM

In the example in the void Teleport() you can see the assigned value of the teleport position.

   PlayerController.pc.myTransform.position = new Vector3(10f, 10f, 0f);

If you want to see PlayerController script here it is:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
 public static float playerSpeed = 5f;
 public GameObject player;
 public Transform myTransform;
 public static PlayerController pc;    
 public static Movement move;
 
 
     // Use this for initialization
     void Start () {
     
     pc=this;

     // Instantiate gamemode (type of rotation)    
     move = new MovementRound();
 
         
         
     myTransform = transform;
         
     // Instantiate position
     myTransform.position = new Vector3(0,0,0);
     
     
                 
     }
     
     // Update is called once per frame
     void Update() {
     
     //Rotation
     move.Move();
         
     //Translation    
     myTransform.Translate (Vector3.left * Time.deltaTime * playerSpeed);
             
         
     }
     
     //Destroy object
     void OnTriggerEnter(Collider collider){
         if (collider.gameObject.CompareTag ("Maze")){
             Debug.Log ("Collision");
             Destroy(gameObject);
         }                
     }
 }
Comment
Add comment · Show 8 · 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 hamstar · Oct 01, 2013 at 09:13 AM 0
Share

$$anonymous$$ight be a stupid question but is PlayerController component attached to the player object or another? Just unsure because if that was the case you wouldn't need public GameObject player;.

avatar image orcinusdev · Oct 01, 2013 at 09:21 AM 0
Share

I haven't cleaned my code really. Ofcourse the PlayerController script is attached to the player object. I realized, when I delete the translate code:

myTransform.Translate (Vector3.left Time.deltaTime playerSpeed);

The teleport works just fine and moves the player to the correct position (this if I drag the teleport object over the player, as the player now don't move). So I guess, transform.Translate and transform.position are not friends? Or have I written my translation code in a wrong way?

avatar image hamstar · Oct 01, 2013 at 09:37 AM 0
Share

Since your Translate is executing in every Update (every frame), it's moving the player constantly. This means the player will be moved to the teleport position, but will carry on moving as you apply the Translate, so it wont stay there. It depends what you wanted to achieve with Translate. You probably want to check for Input and then apply the translate (or forces) only when the user is pressing certain buttons.

avatar image orcinusdev · Oct 01, 2013 at 09:47 AM 0
Share

I thought about this, and it seems correct. But can I make this work then? So that my player starts to move right after teleportation just as before teleportation. I also tried setting the translation to Vector3.zero, but it gave me the same incorrect results as using Vector3.left, diffrent position everytime. Ideas? Thankfull for your answers

avatar image hamstar · Oct 01, 2013 at 10:32 AM 0
Share

If you want the player to teleport to Vector3(10f, 10f, 0f) and then keep moving, the code seems O$$anonymous$$. But I might be missing something. Of course you would need to remove the line playerSpeed = 0 in Teleport(). Also have a look over what's happening in $$anonymous$$ove() I guess?

Show more comments

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

15 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

Related Questions

How to transform position? 2 Answers

transform position player teleport 0 Answers

Transform position? 2 Answers

I'm trying to teleport with a key press, but right now they only teleport for one second. How to fix?,With a key press, no OnTriggerEnter, how to teleport the player? 2 Answers

Making The Hole In A Golf Game 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