I have problems with playerController in endless runner

hello, I have been developing recently and when I was doing an endless runner (like the SubwaySerfers game), there was a problem when swiping left or right (mobile game), the character does not move, but when swiping and jumping, it moves, please help me, I don’t know what to do (code below)

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class PlayerController : MonoBehaviour

{

private CharacterController controller;

private Animator anim;

private Vector3 dir;

[SerializeField] private float speed;

[SerializeField] private float jumpForce;

[SerializeField] private float gravity;

[SerializeField] private int coins;

[SerializeField] private GameObject losePanel;

[SerializeField] private Text coinsText;

private int lineToMove = 1;

public float lineDistance = 4;

private float maxSpeed = 80;

void Start()

{

anim = GetComponentInChildren();

controller = GetComponent();

StartCoroutine(SpeedIncrease());

}

private void Update()

{

if (SwipeController.swipeRight)

{

if (lineToMove < 2)

lineToMove++;

}

if (SwipeController.swipeLeft)

{

if (lineToMove > 0)

lineToMove–;

}

if (SwipeController.swipeUp)

{

if (controller.isGrounded)

Jump();

}

if (controller.isGrounded)

anim.SetTrigger(“isRunning”);

Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;

if (lineToMove == 0)

targetPosition += Vector3.left * lineDistance;

else if (lineToMove == 2)

targetPosition += Vector3.right * lineDistance;

if (transform.position == targetPosition)

return;

Vector3 diff = targetPosition - transform.position;

Vector3 moveDir = diff.normalized * 25 * Time.deltaTime;

if (moveDir.sqrMagnitude < diff.sqrMagnitude)

controller.Move(moveDir);

else

controller.Move(diff);

}

private void Jump()

{

dir.y = jumpForce;

anim.SetTrigger(“isJumping”);

}

void FixedUpdate()

{

dir.z = speed;

dir.y += gravity * Time.fixedDeltaTime;

controller.Move(dir * Time.fixedDeltaTime);

}

private void OnControllerColliderHit(ControllerColliderHit hit)

{

if (hit.gameObject.tag == “obstacle”)

{

losePanel.SetActive(true);

Time.timeScale = 0;

}

}

private void OnTriggerEnter(Collider other)

{

if (other.gameObject.tag == “Coin”)

{

coins++;

coinsText.text = coins.ToString();

Destroy(other.gameObject);

}

}

private IEnumerator SpeedIncrease()

{

yield return new WaitForSeconds(3);

if (speed < maxSpeed)

{

speed += 1;

StartCoroutine(SpeedIncrease());

}

}

Fix your code to use code tags. Get rid of all those huge spaces between lines.

Yes, this… How to use code tags: https://discussions.unity.com/t/481379

This one can be annoying: it may be necessary to Paste And Match Style to get rid of these extra spaces.

Beyond that, here’s some more useful information:

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

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 are the values of the variables involved? Are they initialized?

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