I am fairly new to Unity and coding in general. I want to make a game where the movement system is only by teleporting between specific set points. I wrote this code and I feel like it can be easily condensed, but I could not figure it out. Also, I have each of the telepads tagged as telepad and their name numbered accordingly to their key value. I tried using a system that added each telepad to the list based on their tag, but it wouldn’t account for order. Can someone help me?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class TeleportOnKey : MonoBehaviour {
public GameObject[] telePads;
public void Update() {
if (Input.GetKeyUp (KeyCode.Alpha1)) {
transform.position = telePads [0].transform.position;
}
if (Input.GetKeyUp (KeyCode.Alpha2)) {
transform.position = telePads [1].transform.position;
}
if (Input.GetKeyUp (KeyCode.Alpha3)) {
transform.position = telePads [2].transform.position;
}
if (Input.GetKeyUp (KeyCode.Alpha4)) {
transform.position = telePads [3].transform.position;
}
transform.position = new Vector3 (transform.position.x, 1, transform.position.z);
}
}
If you really need to make your code shorter, try this :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[System.Serializable]
public struct TelePad
{
public Transform transform ;
public KeyCode keyCode;
}
public class TeleportOnKey : MonoBehaviour
{
public TelePad[] telePads;
public void Update() {
for( int i = 0 ; i < telePads.Length ; ++i )
if( Input.GetKeyUp (telePads*.keyCode) )*
transform.position = new Vector3( telePads_.transform.x, 1, telePads*.transform.z ) ;
}
}*
However, keep in mind that having few lines of code won’t make your program run faster. Also, it may confuse your co-workers (or yourself in the future) because your code is harder to read / to understand (Don’t forget to add comments)_
Wow… You gave me a hard time with this!
I managed to find a way of doing this, but you will need to use List instead of Array. (Don’t worry, Lists are better!! )
Let me explain you the thing a bit, the script is at the end.
- You need to create a List with all the telepads in it, then sort this list in void Start or Awake.
- Now you will need to detect what key was pressed and Isolate the number from the KeyCode.
- Then convert this string to int and get the last digit (ex: Alpha3 would be 3)
- Finally, use the last digit and teleport to the proper telepad!
**PS:
I did some research because I really wanted to figure it out! But man this was complicated! If my script works for you, please be sure to set my reply as answer and vote for it 
Here’s the Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Add these Libraries
using System;
using System.Text.RegularExpressions;
using System.Linq;
//Put this script on the object that needs to be teleported!
public class TeleportToTelepad : MonoBehaviour
{
List<GameObject> telePads = new List<GameObject>();
KeyCode requestedKey;
void Start ()
{
//Capture all the telePads in a List
telePads = GameObject.FindGameObjectsWithTag("telepad").ToList();
//Then sort them to be in order
telePads.Sort((x, y) => string.Compare(x.name, y.name));
}
void Update()
{
if (Input.anyKey)
{
foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(vKey))
{
//Let's grab the current number value of the key
String requestedKey = vKey.ToString();
//Isolate the digit
string lastDigit = Regex.Match(requestedKey, @"\d+").Value;
//Then lets convert it as an int
Int32 valAsInt;
if (Int32.TryParse(lastDigit, out valAsInt))
{
//Check if Valid
if (valAsInt >= 0 && valAsInt <= 9)
{
Debug.Log("Teleported to " + valAsInt + " telePad Position");
transform.position = telePads[valAsInt].transform.position;
transform.position = new Vector3(transform.position.x, 1, transform.position.z);
}
}
}
}
}
}
}
My Pleasure!