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;
}
}