Well, I don’t know what happened but I was working and the character move correctly, then I closed the Unity and today I opened it and the glitch started.
I tried to remove the script and put it on again but it doesn’t work, I also tried removing the capsule (character) but it still not working.
The capsule have the capsule collider, the script an the character controller.
I’m using Unity 3D 2018.4.36f1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private CharacterController controller;
private Vector3 direction;
public float forwardSpeed;
private int desiredLane = 1; // 0: izquierda 1: medio 2: derecha
public float laneDistance = 4; // distancia entre carriles
public float jumpForce;
public float Gravity = -20;
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
direction.z = forwardSpeed;
if (controller.isGrounded)
{
direction.y = -1;
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Jump();
}
}
else
{
direction.y += Gravity * Time.deltaTime;
}
//Carril deseado
if (Input.GetKeyDown(KeyCode.RightArrow))
{
desiredLane++;
if (desiredLane == 3)
desiredLane = 2;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
desiredLane--;
if (desiredLane == -1)
desiredLane = 0;
}
//Calcular donde deberíamos de terminar al movernos
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 = Vector3.Lerp(transform.position, targetPosition, 80 * Time.deltaTime);
}
private void FixedUpdate()
{
controller.Move(direction * Time.fixedDeltaTime);
}
//Jump
private void Jump()
{
direction.y = jumpForce;
}
}