- Home /
Only Teleports Player Every Once in While
Snippet of Teleport.cs
where it isn't working:
Vector3 point = new Vector3(hitxz.point.x, hity.point.y, hitxz.point.z);
if (teleport_example == null) {
teleport_example = Instantiate(teleport_example_prefab, point, new Quaternion(0, 0, 0, 0));
} else {
teleport_example.transform.position = point;
}
if (Input.GetMouseButtonDown(0)) {
transform.position = point;
Debug.Log("SDFSDF");
}
The weird problem when this code runs is that 'teleport_example.transform.position = point;` works just fine, but when I click the left mouse button, 'transform.position = point;' only works every 3 - 30 times. Also, the console shows "SDFSDF" being printed, but the player isn't being teleported! I tried checking to see if gameObject was null for some odd reason, and it's not.
Teleport.cs
is a component of player, and the code is being run in Update()
Please tell me if you need any more information, and thanks in advance.
Answer by Coulomb1 · Aug 29, 2021 at 05:44 PM
I found the solution! It actually was the CharacterController script for some odd reason. The fix was to disable to CharacterController script before I changed transform.position then re-enable it afterwards.
Here is the final portion:
point = new Vector3(hitxz.point.x, hity.point.y, hitxz.point.z);
if (teleport_example == null) {
teleport_example = Instantiate(teleport_example_prefab, point, new Quaternion(0, 0, 0, 0));
} else {
controller.enabled = false;
teleport_example.transform.position = point;
controller.enabled = true;
}
if (Input.GetMouseButtonDown(0)) {
point.y += 1;
transform.position = point;
}
Thank you to @GeroNL for helping me find the solution.
Answer by GeroNL · Aug 27, 2021 at 02:50 AM
Hello, can you share hitxz.point when being set? you can check with Debug the player pos and point pos in button to check properly, like this:
if (Input.GetMouseButtonDown(0)) {
Debug.Log("check MouseDown, transform.position :"+transform.position .x+", "+ transform.position.y+", "transform.position.z);
Debug.Log("check MouseDown, pos of point:"+point.x+", "+ point.y+", "pointz);
transform.position = point;
Debug.Log("SDFSDF");
}
If it on wrong pos, then you can check it's point get set properly or not.
hitxz is a RaycastHit, and this is how it is set:
RaycastHit hitxz;
if (Physics.Raycast(new Vector3(hitxz.point.x, hitxz.point.y + 0.01f, hitxz.point.z), Vector3.down, out hity, Mathf.Infinity)) {
...
...
if (Input.GetMouseButtonDown(0)) {
Debug.Log("check MouseDown, transform.position :" + transform.position.x + ", " + transform.position.y + ", " + transform.position.z);
Debug.Log("check MouseDown, pos of point:" + point.x + ", " + point.y + ", " + point.z);
transform.position = point;
Debug.Log("SDFSDF");
}
} Also, this is the result of your code:
Are teleport and player separated?,. im sure the gameobject of teleport already set if the point is correct, you can check when game running if gameobect of teleport already set in tranform, seem with value of debug.
Teleport is a component of player, so there should be no problem there.
Also, I found that after I change transform.position, transform.position changes (shocker), but at the next frame, it changes back!
I have a simple wasd movement controller that is run by a CharacterController, but I don't think that should be the issue.