Quantcast
Viewing latest article 22
Browse Latest Browse All 52

Answer by M-G-Production

**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: - You can't have more than 10 telepads in your list usiing this method! - Telepads must be named like this: Telepad (0); Telepad(1); Telepad(2)... etc 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 telePads = new List(); 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!

Viewing latest article 22
Browse Latest Browse All 52

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>