- Home /
Teleport Script by KeyDown for fast Level testing
Hi!
I have a problem with a C# Script which is made for faster level testing.
How it works:
You can make multiple empty GameObjects(Teleport points for now) and place them freely around the map and attaching the below script to them. After that you can choose which button should Teleport the Player to which GameObject.
My problem is very weird because the Script is works under 2017.2.5 just fine, but on 2019.2.5 when I try to teleport I instantly get back where I was.
I searched a lot but couldn't figured out myself, if you have any idea please share, I would appreciate a lot!
UPDATE:
I think I found where I should look into, I suppose the movement controller what I use Updates more frequently?(I'm not a coder) and rewrites this script all the time ? If I change the Update method to FixedUpdate, the code is working but only for one teleport point so the next step is how I should modify the code to enable the other teleport points too ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestTeleportPoint : MonoBehaviour {
[Header("Alpha is numbers on keyboard and numpad are numbers on the numpad")]
[SerializeField]
private Keys button;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown((KeyCode)button))
{
GameObject.FindWithTag("Player").transform.position = transform.position;
}
}
private enum Keys
{
Alpha1 = KeyCode.Alpha1,
Alpha2 = KeyCode.Alpha2,
Alpha3 = KeyCode.Alpha3,
Alpha4 = KeyCode.Alpha4,
Alpha5 = KeyCode.Alpha5,
Alpha6 = KeyCode.Alpha6,
Alpha7 = KeyCode.Alpha7,
Alpha8 = KeyCode.Alpha8,
Alpha9 = KeyCode.Alpha9,
Alpha0 = KeyCode.Alpha0,
Numpad0 = KeyCode.Keypad0,
Numpad1 = KeyCode.Keypad1,
Numpad2 = KeyCode.Keypad2,
Numpad3 = KeyCode.Keypad3,
Numpad4 = KeyCode.Keypad4,
Numpad5 = KeyCode.Keypad5,
Numpad6 = KeyCode.Keypad6,
Numpad7 = KeyCode.Keypad7,
Numpad8 = KeyCode.Keypad8,
Numpad9 = KeyCode.Keypad9,
}
}
Answer by altan86 · Jun 17, 2020 at 11:22 AM
Hey everyone, I fixed this problem with a workaround and that is to: 1. disable the player character controller, 2. apply the transform modifier to the player Transform, and 3. enable the player character controller.
CharacterController player = FindObjectOfType<CharacterController>();
player.enabled = false;
player.transform.SetPositionAndRotation(transform.position,transform.rotation);
player.enabled = true;
Apparently, in 2017 the character controller doesn't force the player transform values back to its current position whereas in 2019 it does that now when you apply a transform value to a GameObject with a character controller component and calling CharacterController.Move in the Update() loop.
Answer by MenimalsEntertainment · Jun 03, 2020 at 05:18 AM
@Atis Instead of setting the player's position to transform.position, create a new Vector3 based on transform.position. It would look like this:
GameObject.FindWithTag("Player").transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z);
@skywalker31415 thanks for the advice and I think I found where I should look into, I suppose the movement controller what I use Updates more frequent?(I'm not a coder) and rewrites this script all the time...
Now if I change the Update method to FixedUpdate it works but only for one teleport point, so the next question is what I should change to enable the other teleport points too ?
I definitely use Update() a lot, especially for things such as movement that require constant updates. I think something to look into is having a controller code that manages all of the teleport points. Every teleport point (represented by the script) can be added to a list and all of them managed through a single code and a single Update() function. Otherwise, if the teleport points don't work, I would use Debug.Log to ensure that all the points are being set properly and see if the problem lies in setting the points or in changing the player's position.
Yeah, I think I have something wrong with the player controller, but I don't understand why yet... because it's the same code as was in 2017 version, maybe they changed something program$$anonymous$$g related in the 2019 versions ?
Your answer
Follow this Question
Related Questions
How to make enemy teleport right next to player? 4 Answers
Spawn different Player models 0 Answers
Skyrim style door teleport 0 Answers
UNET Players not spawning on server change scene 1 Answer
Add spawn to script [Multiplayer] 0 Answers