Search Unity

Teleport / Respawn help

Discussion in 'Scripting' started by Drix_, Jan 7, 2019.

  1. Drix_

    Drix_

    Joined:
    Sep 12, 2018
    Posts:
    3
    So I'm trying to have my character teleport / respawn when a condition is met. However, I'm having a problem with changing the character position. I set it up so when I press 'R' it moves the characters position to the position I specified. However, it doesn't always work the first time I press 'R'. Sometimes I have to press it upwards of 10 times for my character to finally move, however everytime I press the button the character does move to the position for a split second but then 'teleports' right back to where it was originally standing.

    Here's the code I'm using to change my characters position. (attached to my player object)
    Code (CSharp):
    1. private Transform _respawn;
    2. public Transform playerTransform;
    3.  
    4. void Start(){
    5.    _respawn = GameObject.Find("Respawn").transform;
    6.    playerTransform = this.gameObject.transform;
    7. }
    8.  
    9. void Update(){
    10.    if (Input.GetKeyDown(KeyCode.R))
    11.       {
    12.           playerTransform.transform.position = _respawn.transform.position;
    13.       }
    Here is a gif that also shows the problem in action, you will notice the camera jitter each time I press the teleport button, and the character moves right back to its original position.


    EDIT:
    After having another set of eyes look at the problem and keeping the controller possibly being the issue in mind I've found a fix.
    Disabling the character controller, then teleporting, then re-enabling it seems to work like a charm.
    Here is the updated code
    Code (CSharp):
    1. private Transform _respawn;
    2. public Transform playerTransform;
    3.  
    4. void Start(){
    5.    _respawn = GameObject.Find("Respawn").transform;
    6.    playerTransform = this.gameObject.transform;
    7. }
    8.  
    9. void Update(){
    10.    if (Input.GetKeyDown(KeyCode.R))
    11.       {
    12.           _characterController.enabled = false;
    13.           playerTransform.transform.position = _respawn.transform.position;
    14.           _characterController.enabled = true;
    15.       }
    16. }
     
    Last edited: Jan 7, 2019
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Something else must be setting the position every frame. Can we see the rest of the code?
     
    Drix_ likes this.
  3. Drix_

    Drix_

    Joined:
    Sep 12, 2018
    Posts:
    3
    The only other thing I have in my Update method is my Movement method, check it out.
    Code (CSharp):
    1. private void Movement()
    2.     {
    3.         float horizontalInput = Input.GetAxis("Horizontal");
    4.         float verticalInput = Input.GetAxis("Vertical");
    5.  
    6.         Vector3 camForward_Dir = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
    7.         Vector3 move = verticalInput * camForward_Dir + horizontalInput * Camera.main.transform.right;
    8.  
    9.         if (move.magnitude > 1f)
    10.         {
    11.             move.Normalize();
    12.         }
    13.  
    14.         move = transform.InverseTransformDirection(move);
    15.         float turnAmount = Mathf.Atan2(move.x, move.z);
    16.         transform.Rotate(0, turnAmount * rotationSpeed * Time.deltaTime, 0);
    17.  
    18.         if (_characterController.isGrounded)
    19.         {
    20.             anim.SetFloat("BlendX", horizontalInput);
    21.             anim.SetFloat("BlendY", verticalInput);
    22.             anim.SetBool("isJumping", false);
    23.  
    24.             _moveDir = transform.forward * move.magnitude;
    25.             _moveDir *= speed;
    26.  
    27.             _verticalVelocity = -_gravityJump * Time.deltaTime;
    28.  
    29.             if (Input.GetButtonDown("Jump"))
    30.             {
    31.                 _verticalVelocity = jumpForce;
    32.                 anim.SetBool("isJumping", true);
    33.             }
    34.         }
    35.         else
    36.         {
    37.             _verticalVelocity -= _gravityJump * Time.deltaTime;
    38.         }
    39.  
    40.         Vector3 jumpVector = new Vector3(0, _verticalVelocity, 0);
    41.         _characterController.Move(jumpVector * Time.deltaTime);
    42.  
    43.         _moveDir.y -= _gravity * Time.deltaTime;
    44.         _characterController.Move(_moveDir * Time.deltaTime);
    45.     }
     
  4. Drix_

    Drix_

    Joined:
    Sep 12, 2018
    Posts:
    3
    I believe the issue resides somewhere in the movement method because calling it after the if statement looking for the key press movement + teleporting stops working completely.
     
  5. T5Shared

    T5Shared

    Joined:
    Oct 19, 2018
    Posts:
    152
    Probably _characterController is setting the transform position, as GroZZleR mentioned
     
    Drix_ likes this.