Search Unity

Question Help with teleporting player to another location. Player cant move after teleporting.

Discussion in '2D' started by AlliSei, Mar 20, 2023.

  1. AlliSei

    AlliSei

    Joined:
    Jan 13, 2023
    Posts:
    2
    I'm very new to Unity. Right now I'm trying to make my character teleport to a new location after collecting a certain amount of Coins. the vector3 code worked perfectly fine before on the roll a ball tutorial. I even tried Vector2 if that was the issue. Still nothing. I don't know what is causing it to not work.

    Sorry if the code is messy. I'm using code from my previous projects (I'm taking a beginners game dev class). I got most of what I need working. Its this teleporting issue that is my biggest issue. I tried everything out there and keeps doing the same thing. I'll also upload a video of it. Start at 0:20 for a better view of it. As soon as I collect the required amount of coins to teleport, it teleports me, but gets me stuck and it keeps teleporting me back to the teleported spot.





    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class playermovement : MonoBehaviour
    8. {
    9.     public CharacterController2D Controller;
    10.  
    11.     float horizontalMove = 0f;
    12.  
    13.     public float runSpeed = 40f;
    14.  
    15.     bool jump = false;
    16.  
    17.     Animator anim;
    18.  
    19.     public Text score;
    20.  
    21.     private int scoreValue;
    22.  
    23.     public Text livesText;
    24.  
    25.     public Text scoreValueText;
    26.     public GameObject winTextObject;
    27.  
    28.     public GameObject loseTextObject;
    29.  
    30.  
    31.     private Rigidbody rb;
    32.  
    33.  
    34.  
    35.     private int lives;
    36.  
    37.  
    38.     void Start()
    39.     {
    40.         anim = GetComponent<Animator>();
    41.  
    42.         score.text = scoreValue.ToString();
    43.         score.text = lives.ToString();
    44.         rb = GetComponent<Rigidbody>();
    45.  
    46.      
    47.         winTextObject.SetActive(false);
    48.  
    49.         lives = 3;
    50.  
    51.         SetlivesText();
    52.  
    53.         loseTextObject.SetActive(false);
    54.  
    55.     }
    56.  
    57.  
    58.     void Update()
    59.     {
    60.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    61.  
    62.         if (Input.GetButtonDown("Jump"))
    63.         {
    64.             jump = true;
    65.             anim.SetInteger("State", 2);
    66.         }
    67.         if (Input.GetButtonUp("Jump"))
    68.         {
    69.             anim.SetInteger("State", 2);
    70.         }
    71.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    72.         {
    73.             anim.SetInteger("State", 2);
    74.         }
    75.         if (Input.GetKeyUp(KeyCode.LeftArrow))
    76.         {
    77.             anim.SetInteger("State", 0);
    78.         }
    79.         if (Input.GetKeyDown(KeyCode.RightArrow))
    80.         {
    81.             anim.SetInteger("State", 2);
    82.         }
    83.         if (Input.GetKeyUp(KeyCode.RightArrow))
    84.         {
    85.             anim.SetInteger("State", 0);
    86.         }
    87.         if (lives <= 2)
    88.         {
    89.             Destroy(gameObject);
    90.         }
    91.      
    92.          
    93.     }
    94.  
    95.     void FixedUpdate ()
    96.     {
    97.         Controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
    98.         jump = false;
    99.     }
    100.  
    101.     private void OnCollisionEnter2D(Collision2D collision)
    102.     {
    103.        if (collision.collider.tag == "Coin")
    104.         {
    105.             scoreValue += 1;
    106.             score.text = scoreValue.ToString();
    107.             Destroy(collision.collider.gameObject);
    108.         }
    109.         else if (collision.collider.tag == "Enemy")
    110.         {
    111.             lives -= 1;
    112.             livesText.text = lives.ToString();
    113.             Destroy(collision.collider.gameObject);
    114.         }
    115.         if (scoreValue == 1)
    116.         {
    117.             gameObject.transform.position = new Vector3(10.0f, 3.0f, 0.0f);
    118.         }
    119.      
    120.     }
    121.  
    122.  
    123.     void SetscoreValueText()
    124.     {
    125.         scoreValueText.text = "score: " + score.ToString();
    126.  
    127.         if (scoreValue >= 2)
    128.         {
    129.             // Set the text value of your 'winText'
    130.             winTextObject.SetActive(true);
    131.         }
    132.  
    133.     }
    134.     void SetlivesText()
    135.     {
    136.         livesText.text = "lives: " + lives.ToString();
    137.  
    138.         if (lives <= 2)
    139.         {
    140.             loseTextObject.SetActive(true);
    141.         }
    142.     }
    143.  
    144. }
    145.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,737
    Usually when you have a fire-only-once effect like that you either Destroy() the script responsible for the effect after it happens (eg, after the teleport happens), or perhaps set a
    bool
    saying that it has happened, so don't do it again.
     
  3. AlliSei

    AlliSei

    Joined:
    Jan 13, 2023
    Posts:
    2
    Thank you so much! I just added a bool and works perfectly now! I guess I have to work on bools more!