- Home /
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
Can you paste the code where you are assigning the value to PlayerController.
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);
}
}
}
$$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;
.
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?
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.
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
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?
Your answer
Follow this Question
Related Questions
How to transform position? 2 Answers
transform position player teleport 0 Answers
Transform position? 2 Answers
Making The Hole In A Golf Game 1 Answer