- Home /
Hey guys, can you help me out with my colliders.
Ive been trying to make an endless runner game in unity but which a player moves forward automatically and can move from x to y axis while still moving forward and i do not know how to program so i used a templete. and since then, my player stops moving forward when there is no box collider in front of it. it stops when there is a box collider by the sides and even when its at the front. its really fraustrating and i would really need help.Below is the script i used using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewPlayerScript : MonoBehaviour { private CharacterController controller; private Vector3 direction; public float forwardSpeed; public float maxSpeed;
private int desiredLane = 1;
public float laneDistance = 4;
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)
{
if(forwardSpeed < maxSpeed)
forwardSpeed += 0.1f * Time.deltaTime;
direction.y = -1;
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Jump();
}
}
else
{
direction.y += Gravity * Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
desiredLane++;
if (desiredLane == 3)
desiredLane = 2;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
desiredLane--;
if (desiredLane == -1)
desiredLane = 0;
}
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);
}
private void Jump()
{
direction.y = jumpForce;
}
} i really dont know what the problem is.i have the character controller and the sphere collider enabled in my inspector for my character and i have only the box collider enabled for my prefabs pls help me out.