• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Dhanushanka · Aug 14, 2022 at 07:36 AM · scripting problemcheckpoint

Teleporting

Hi, I want to teleport the player in to a certain location in certain conditions. The variable "value" is the checkpoint. When collided with an object I need to teleport to a checkpoint depending on the "value". The script is currently teleporting me to the 5th checkpoint.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class AI : MonoBehaviour
 {
     [Header("Text and Setup")]
     public TextMeshProUGUI dialougeText; // The AI Dialouge Box Text
     public GameObject player; // The player
 
     [Header("Trigger Settings")] // Triggers
     public GameObject fallBorder;
     public GameObject trigger0;
     public GameObject trigger1;
     public GameObject trigger2;
     public GameObject trigger3;
     public GameObject trigger4;
     public GameObject trigger5;
 
     [Header("Dialouge Settings")] // Text Settings
     public string text0;
     public string text1;
     public string text2;
     public string text3;
     public string text4;
     public string text5;
 
     [Header("Script Debugging")]
     public int value;
 
     void Start()
     {
         dialougeText = dialougeText.GetComponent<TextMeshProUGUI>(); // Get The Component
     }
     public void OnTriggerEnter(Collider other) // On Trigger Collision
     {
         if (other.gameObject.name == fallBorder.name) // If the tag is ...
         {
             Debug.Log("Player falled!"); // Trigger Confirm
             Teleport(); // Teleport
         }
 
         if (other.gameObject.name == trigger0.name) // If the name is ...
         {
             Debug.Log("Trigger0 enter"); // Trigger Confirm
             dialougeText.text = (text0); // Set Text
             value = 0;
         }
 
         if (other.gameObject.name == trigger1.name) // If the name is ...
         {
             Debug.Log("Trigger1 enter"); // Trigger Confirm
             dialougeText.text = (text1); // Set Text
             value = 1;
         }
 
         if (other.gameObject.name == trigger2.name) // If the name is ...
         {
             Debug.Log("Trigger2 enter"); // Trigger Confirm
             dialougeText.text = (text2); // Set Text
             value = 2;
         }
 
         if (other.gameObject.name == trigger3.name) // If the name is ...
         {
             Debug.Log("Trigger3 enter"); // Trigger Confirm
             dialougeText.text = (text3); // Set Text
             value = 3;
         }
 
         if (other.gameObject.name == trigger4.name) // If the name is ...
         {
             Debug.Log("Trigger4 enter"); // Trigger Confirm
             dialougeText.text = (text4); // Set Text
             value = 4;
         }
 
         if (other.gameObject.name == trigger5.name) // If the name is ...
         {
             Debug.Log("Trigger5 enter"); // Trigger Confirm
             dialougeText.text = (text5); // Set Text
             value = 5;
         }
     }
 
     public void Teleport()
     {
         player.transform.position = trigger5.transform.position;
     }
 }
 
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by New_Game_Ideas · Aug 14, 2022 at 10:33 AM

First I want to mention that it is a liitle tricky using an int. It is better to make your "value" variable Transform or Game object. But if you still want to leave it as int chnage your teleport funciton to this:

    public void Teleport()
         {
             switch (value)
             {
                 default:
                     break;
                 case 0:
                     gameObject.transform.position = trigger0.transfrom.position;
                     break;
                 case 1:
                     gameObject.transform.position = trigger1.transfrom.position;
                     break;
                 case 2:
                     gameObject.transform.position = trigger2.transfrom.position;
                     break;
                 case 3:
                     gameObject.transform.position = trigger3.transfrom.position;
                     break;
                 case 4:
                     gameObject.transform.position = trigger4.transfrom.position;
                     break;
                 case 5:
                     gameObject.transform.position = trigger5.transfrom.position;
                     break;
             }
         }

However this cannot consists fallBorder. You need to do it with if statement.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

248 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A problem regarding to player animation with checkpoint in 2D platformer 0 Answers

How to increase number of movement in one swipe input ? 1 Answer

Moving a ScrollRect child via script to focus on some point. 0 Answers

I am making a click based game. I need to print out a log of the actions(clicks) made by the person and store that log to be used later. How can i do that? 1 Answer

How to get accurate x/y for Rectangle Transform of Panel under canvas? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges