Trying to move the Player 30 units on the x axis back and forth when clicking but transform.Translate not working reliably?

I’ve been trying to learn Unity and followed some tutorials and have been reading the documentation and I’m trying to teleport the Player when you click, it works a lot of the time but not all the time, sometimes you have to click a few times for it to actually happen. Any ideas what I could do to fix this?


using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
	public CharacterController controller;

	public Transform groundCheck;
	public float groundDistance;

	public float jumpHeight = 3f;

	public LayerMask groundMask;

	public float speed = 12f;
	public float gravity = -9.81f;

	Vector3 velocity;
	Vector3 moveRightVector = new Vector3(30f, 0f, 0f);
	Vector3 moveLeftVector = new Vector3(-30f, 0f, 0f);
	Vector3 playerStop = new Vector3(0f, 0f, 0f);

	bool isGrounded;

	private void GravityEffect()
    {
		float localGravity = gravity;

		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

		controller.Move(velocity * Time.deltaTime);
		velocity.y += localGravity * Time.deltaTime;

		if (isGrounded && velocity.y < 0)
		{
			velocity.y = -2f;
		}
	}
	private void PlayerMove()
    {
		float x = Input.GetAxis("Horizontal");
		float z = Input.GetAxis("Vertical");

		Vector3 move = transform.right * x + transform.forward * z;

		controller.Move(move * speed * Time.deltaTime);
	}

	private void PlayerJump()
    {
		float localGravity = gravity;

		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

		controller.Move(velocity * Time.deltaTime);

		if (Input.GetButtonDown("Jump") && isGrounded)
		{
			velocity.y = Mathf.Sqrt(jumpHeight * -2f * localGravity);
		}
	}

	private void PlayerTeleport()
	{
		if (transform.position.x < 14)
		{
			transform.Translate(moveRightVector, Space.World);
		} else if (transform.position.x > 15)
        {
			transform.Translate(moveLeftVector, Space.World);
        }
	}


	void Update()
	{
		if (Input.GetButtonDown("Fire1"))
		{
			PlayerTeleport();
		} 
		else if (!Input.GetButtonDown("Fire1"))
        {
			PlayerMove();
			PlayerJump();
			GravityEffect();
        }
	}
}

If anyone else comes across this problem, I solved it by disabling the Character Controller when teleporting :slight_smile:


using System.Collections;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
	public CharacterController controller;

	public Transform groundCheck;
	public float groundDistance;

	public float jumpHeight = 3f;

	public LayerMask groundMask;

	public float speed = 12f;
	public float gravity = -9.81f;

	Vector3 velocity;
	Vector3 moveRightVector = new Vector3(30f, 0f, 0f);
	Vector3 moveLeftVector = new Vector3(-30f, 0f, 0f);
	Vector3 playerStop = new Vector3(0f, 0f, 0f);

	bool isGrounded;

	private void PlayerGravity()
    {
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

		controller.Move(velocity * Time.deltaTime);
		velocity.y += gravity * Time.deltaTime;

		if (isGrounded && velocity.y < 0)
		{
			velocity.y = -2f;
		}
	}
	private void PlayerMove()
    {
		float x = Input.GetAxis("Horizontal");
		float z = Input.GetAxis("Vertical");

		Vector3 move = transform.right * x + transform.forward * z;

		controller.Move(move * speed * Time.deltaTime);
	}

	private void PlayerJump()
    {
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

		controller.Move(velocity * Time.deltaTime);

		if (Input.GetButtonDown("Jump") && isGrounded)
		{
			velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
		}
	}

	private void PlayerTeleport()
	{
		if (transform.position.x < 14)
		{
			transform.Translate(moveRightVector, Space.World);
		} else if (transform.position.x > 14)
        {
			transform.Translate(moveLeftVector, Space.World);
        }
	}


	void Update()
	{
		if (Input.GetButtonDown("Fire1"))
		{
			controller.enabled = false;
			PlayerTeleport();
		} 
		else if (!Input.GetButtonDown("Fire1"))
        {
			controller.enabled = true;
			PlayerMove();
			PlayerJump();
			PlayerGravity();
        }
	}
}