My character is misguiding in the game as time passes. Can you help me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : MonoBehaviour
{
private const float LANE_DISTANCE = 2.3f;
private const float TURN_SPEED = 0.05f;
// Func
private bool isRunning = false;
// Falling
// Animation
private Animator anim;
//Movement
private CharacterController controller;
private readonly float jumpForce = 4.70f;
private readonly float gravity = 12.0f;
private float verticalVeloCity;
private int desiredLane = 1; //0=left,1=middle,2=right
// Speed Modifier
private readonly float originalSpeed = 7.0f;
private float speed;
private float speedIncreaseLastTick;
private readonly float speedIncreaseTime = 2.5f;
private readonly float speedIncreaseAmaount = 0.10f;
private void Start()
{
speed = originalSpeed;
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
private void Update()
{
if (!isRunning)
return;
if (Time.time - speedIncreaseLastTick > speedIncreaseTime)
{
speedIncreaseLastTick = Time.time;
speed += speedIncreaseAmaount;
// Change Modifer Text
GameManager.Instance.UpdateModifer(speed - originalSpeed);
}
// Gather the inputs on whic lane we should be
if (MobileInput.Instance.SwipeLeft)
MoveLane(false);
if (MobileInput.Instance.SwipeRight)
MoveLane(true);
// Calculate where we should be in the future
Vector3 targetPosition = transform.position.z * Vector3.forward;
if (desiredLane == 0)
targetPosition += Vector3.left * LANE_DISTANCE;
else if (desiredLane == 2)
targetPosition += Vector3.right * LANE_DISTANCE;
// Let's calcu our move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
bool isGrounded = IsGrounded();
anim.SetBool("Grounded", isGrounded);
// Calculate Y
if (isGrounded)// İf Grounded
{
verticalVeloCity = -0.1f;
if (MobileInput.Instance.SwipeUp)
{
//JUMP
anim.SetTrigger("jump");
verticalVeloCity = jumpForce;
}
else if (MobileInput.Instance.SwipeDown)
{
// Slide
StarSliding();
Invoke(nameof(StopSliding), 1.0f);
}
}
else
{
verticalVeloCity -= (gravity * Time.deltaTime);
// Fast falling mechaning
if (MobileInput.Instance.SwipeDown)
{
verticalVeloCity = -jumpForce;
}
}
moveVector.y = verticalVeloCity;
moveVector.z = speed;
// Move the Robot
controller.Move(moveVector * Time.deltaTime);
// Rotate the rabbit to where is going
Vector3 dir = controller.velocity;
if (dir != Vector3.zero)
{
dir.y = 0;
transform.forward = Vector3.Lerp(transform.forward, dir, TURN_SPEED);
}
}
private void StarSliding()
{
anim.SetBool("Sliding", true);
controller.height /= 2;
controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
}
private void StopSliding()
{
anim.SetBool("Sliding", false);
controller.height *= 2;
controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
}
private void MoveLane(bool goingRight)
{
desiredLane += (goingRight) ? 1 : -1;
desiredLane = Mathf.Clamp(desiredLane, 0, 2);
}
private bool IsGrounded()
{
Ray groundRay = new Ray(new Vector3(controller.bounds.center.x, (controller.bounds.center.y - controller.bounds.extents.y) + 0.2f, controller.bounds.center.z), Vector3.down);
Debug.DrawRay(groundRay.origin, groundRay.direction, Color.cyan, 1.0f);
return Physics.Raycast(groundRay, 0.2f + 0.1f);
}
public void StartRunning()
{
isRunning = true;
anim.SetTrigger("StartRunning");
}
private void Crash()
{
anim.SetTrigger("Death");
isRunning = false;
GameManager.Instance.OnDeath();
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
switch (hit.gameObject.tag)
{
case "Obstacle":
Crash();
break;
}
switch (hit.gameObject.tag)
{
case "Falling":
Falling();
break;
}
}
private void Falling()
{
anim.SetTrigger("Falling");
}
}