Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Attempting to teleport player durin / after cutscene

Discussion in 'Scripting' started by RubenVanOostveen, Oct 26, 2021.

  1. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    Im trying to teleport the player however it sets me back to the original location / reset.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CutScene1 : MonoBehaviour
    6. {
    7.   public GameObject CutSceneCam;
    8.   public GameObject Audio;
    9.   public GameObject Collider;
    10.   public GameObject Enemy;
    11.   public GameObject Collider2;
    12.   public GameObject Licht;
    13.   public GameObject Playeraudio;
    14.   public GameObject DoorSound;
    15.   public GameObject Player;
    16.   public GameObject Fade;
    17.   public GameObject VoiceLine_003;
    18.  
    19.   //void Start(){
    20.     //Fade.gameObject.SetActive(false);
    21.   //}
    22.  
    23.     void OnTriggerEnter(Collider other)
    24.   {
    25.     if (other.gameObject.tag == "Player")
    26.     {
    27.       Fade.gameObject.SetActive(false);
    28.       StartCoroutine(Cutscene());
    29.       StartCoroutine(PlayAudio2());
    30.         }
    31.   }
    32.  
    33.     IEnumerator Cutscene()
    34.     {
    35.         Fade.SetActive(true);
    36.         CutSceneCam.SetActive(true);
    37.         Audio.SetActive(true);
    38.         Player.SetActive(false);
    39.         Collider2.SetActive(true);
    40.         Licht.SetActive(true);
    41.         Player.transform.position = new Vector3(267, 15, 375);
    42.         yield return new WaitForSeconds(11.1f);
    43.         Fade.SetActive(false);
    44.         Fade.SetActive(true);
    45.         Player.SetActive(true);
    46.         CutSceneCam.SetActive(false);
    47.         Audio.SetActive(false);
    48.         DoorSound.SetActive(false);
    49.         Enemy.SetActive(true);
    50.         Collider.SetActive(false);
    51.         Collider2.SetActive(false);
    52.         Licht.SetActive(false);
    53.         Playeraudio.SetActive(false);
    54.         // also tried to put it here maybe it needed to be on (the player itself)
    55.         yield return new WaitForSeconds(2f);
    56.         Fade.SetActive(false);
    57.         VoiceLine_003.SetActive(true);
    58.         yield return new WaitForSeconds(3f);
    59.         VoiceLine_003.SetActive(false);
    60.     }
    61.     IEnumerator PlayAudio2()
    62.     {
    63.         Playeraudio.SetActive(false);
    64.         yield return new WaitForSeconds(6.1f);
    65.         Playeraudio.SetActive(true);
    66.  
    67. }
    68. }
    69.  
    Im aware of the errors in the console however they are not connected to this script I'm working on that too. (My friend made that script I'm cleaning up his mess)



    I used this forum to get the teleport code:
    https://answers.unity.com/questions/1470684/how-to-change-position-of-gameobject.html
     
  2. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    Line 41 has the code
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494