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): private Transform _respawn; public Transform playerTransform; void Start(){ _respawn = GameObject.Find("Respawn").transform; playerTransform = this.gameObject.transform; } void Update(){ if (Input.GetKeyDown(KeyCode.R)) { playerTransform.transform.position = _respawn.transform.position; } 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): private Transform _respawn; public Transform playerTransform; void Start(){ _respawn = GameObject.Find("Respawn").transform; playerTransform = this.gameObject.transform; } void Update(){ if (Input.GetKeyDown(KeyCode.R)) { _characterController.enabled = false; playerTransform.transform.position = _respawn.transform.position; _characterController.enabled = true; } }
The only other thing I have in my Update method is my Movement method, check it out. Code (CSharp): private void Movement() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); Vector3 camForward_Dir = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; Vector3 move = verticalInput * camForward_Dir + horizontalInput * Camera.main.transform.right; if (move.magnitude > 1f) { move.Normalize(); } move = transform.InverseTransformDirection(move); float turnAmount = Mathf.Atan2(move.x, move.z); transform.Rotate(0, turnAmount * rotationSpeed * Time.deltaTime, 0); if (_characterController.isGrounded) { anim.SetFloat("BlendX", horizontalInput); anim.SetFloat("BlendY", verticalInput); anim.SetBool("isJumping", false); _moveDir = transform.forward * move.magnitude; _moveDir *= speed; _verticalVelocity = -_gravityJump * Time.deltaTime; if (Input.GetButtonDown("Jump")) { _verticalVelocity = jumpForce; anim.SetBool("isJumping", true); } } else { _verticalVelocity -= _gravityJump * Time.deltaTime; } Vector3 jumpVector = new Vector3(0, _verticalVelocity, 0); _characterController.Move(jumpVector * Time.deltaTime); _moveDir.y -= _gravity * Time.deltaTime; _characterController.Move(_moveDir * Time.deltaTime); }
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.