Character won't move, followed a tutorial

Hi, I’m new to learning coding and unity

but I followed a tutorial on how to make an endless runner game and in one part the instructor just added some new code lines without explaining.

And now with the codes that I wrote from the videos, the character can’t move forward.

So if anyone could help me fix the codes and get the character moving I’d appreciate

This is the CharacterController script that I think causes the problem.

Again, I’m very new so sorry if this is stupid

using System.Collections;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 move;
    public float forwardSpeed;
    public float maxSpeed;

    private int desiredLane = 1;//0:left, 1:middle, 2:right
    public float laneDistance = 2.5f;//The distance between tow lanes

    public bool isGrounded;


    public float gravity = -12f;
    public float jumpHeight = 2;
    private Vector3 velocity;

    public Animator animator;
    private bool isSliding = false;

    public float slideDuration = 1.5f;

    bool toggle = false;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        Time.timeScale = 1.2f;
    }

    private void FixedUpdate()
    {
        if (!PlayerManager.isGameStarted || PlayerManager.gameOver)
            return;

        //Increase Speed
        if (toggle)
        {
            toggle = false;
            if (forwardSpeed < maxSpeed)
                forwardSpeed += 0.1f * Time.fixedDeltaTime;
        }
        else
        {
            toggle = true;
            if (Time.timeScale < 2f)
                Time.timeScale += 0.005f * Time.fixedDeltaTime;
        }
    }

    void Update()
    {
        if (!PlayerManager.isGameStarted || PlayerManager.gameOver)
            return;

        animator.SetBool("isGameStarted", true);
        move.z = forwardSpeed;

        animator.SetBool("isGrounded", isGrounded);
        if (isGrounded && velocity.y < 0)
            velocity.y = -1f;

         if (controller.isGrounded)
        {
            if (SwipeManager.swipeLeft)
                 {
                      desiredLane--;
                      if(desiredLane == -1)
            {
                desiredLane = 0;
            }
        }

        if (SwipeManager.swipeUp)
        {
            Jump();
            animator.SetBool("isGrounded", false);
            }
        else
        {
            move.y += gravity * Time.deltaTime;
            animator.SetBool("isGrounded", true);
        }

        }




        controller.Move(velocity * Time.deltaTime);

        //Gather the inputs on which lane we should be
        if (SwipeManager.swipeRight)
        {
            desiredLane++;
            if (desiredLane == 3)
                desiredLane = 2;
        }
        if (SwipeManager.swipeLeft)
        {
            desiredLane--;
            if (desiredLane == -1)
                desiredLane = 0;
        }

        //Calculate where we should be in the future
        Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
        if (desiredLane == 0)
            targetPosition += Vector3.left * laneDistance;
        else if (desiredLane == 2)
            targetPosition += Vector3.right * laneDistance;

        //transform.position = targetPosition;
        if (transform.position != targetPosition)
        {
            Vector3 diff = targetPosition - transform.position;
            Vector3 moveDir = diff.normalized * 30 * Time.deltaTime;
            if (moveDir.sqrMagnitude < diff.magnitude)
                controller.Move(moveDir);
            else
                controller.Move(diff);
        }

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

    private void Jump()
    {
        StopCoroutine(Slide());
        animator.SetBool("isSliding", false);
        animator.SetTrigger("jump");
        controller.center = Vector3.zero;
        controller.height = 2;
        isSliding = false;

        velocity.y = Mathf.Sqrt(jumpHeight * 2 * -gravity);
    }

    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (hit.transform.tag == "Obstacle")
        {
            PlayerManager.gameOver = true;
            FindObjectOfType<AudioManager>().PlaySound("GameOver");
        }
    }

    private IEnumerator Slide()
    {
        isSliding = true;
        animator.SetBool("isSliding", true);
        yield return new WaitForSeconds(0.25f / Time.timeScale);
        controller.center = new Vector3(0, -0.5f, 0);
        controller.height = 1;

        yield return new WaitForSeconds((slideDuration - 0.25f) / Time.timeScale);

        animator.SetBool("isSliding", false);

        controller.center = Vector3.zero;
        controller.height = 2;

        isSliding = false;
    }
}

Hi @ErfanWASD,

Lets start simple:
(1) Did you assign fowardSpeed and maxSpeed in inspector Tab?

1 Like

That’s why you also need to provide Editor screenshots and so on.
The more info you provide, the faster you will get a solution.

Hi again @ErfanWASD,

If (1) will not help…
(2) go to controller.Move, and add there this line

Debug.Log(_Vecotor3 Which Is Entry Parameter For Move Void);

Then when you expect to go forward, check did Move wass called and how input Vecor3 look like

Hope will do, let us know did you manage to solve your issue

Hello
First off, thank you for your response to my problem
I genuinely don’t know what the problem was but I expect it was some bug related to unity
because after I just copied the code and assigned it again it worked fine

Nah… sometimes coping code is not enough :blush: go with (1) probably you need assign this values because currently you using zeros as them… if this will not help go with (2).

as you can see here:

public float forwardSpeed;
public float maxSpeed;

someone who write it do not assign value. Design them as public to expose them in inspector for you, so you could change values even during Play Mode and check what values will be best

Yea it was zero on the inspector and had to manually change it, but now I’ve integrated it into the code so it starts with a value
Thanks!

1 Like