- Home /
Endless Runner Game: Turn Corners
How do I add corners into my game? with using these scripts. I'm sorry if this is long but I'm new too Unity. I have a tile manager script(Underneath the character controller script), so the game can choose a random path for the player. I also have lane changes(Which you can see in the script), so when the player moves left. They, move to a certain spot, same thing if they went right. But, I don't want my player rotating right or left when the corner tile is not there. Also, once I have the corner tile, I need still need to make sure that the game is still creating a new path for that tile. Someone please help me.
public class PlayerController : MonoBehaviour {
private CharacterController controller;
private Vector3 direction;
public float forwardSpeed;
public float maxSpeed;
private int desiredLane = 1; //0=left 1=middle 2=right
public float laneDistance = 3; // the distance between two lanes
public float jumpForce;
public float Gravity = -20;
public float AddForce;
bool alive = true;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//Increase speed
if(forwardSpeed < maxSpeed)
forwardSpeed += 0.1f * Time.deltaTime;
direction.z = forwardSpeed;
if (controller.isGrounded)
{
direction.y = -1;
if (SwipeManager.swipeUp)
{
Jump();
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Jump();
}
}
else
{
direction.y += Gravity * Time.deltaTime;
}
//Gather the inputs on which lane we should be on
if (SwipeManager.swipeRight)
{
desiredLane++;
if (desiredLane == 3)
desiredLane = 2;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
desiredLane++;
if (desiredLane == 3)
desiredLane = 2;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
desiredLane--;
if (desiredLane == -1)
desiredLane = 0;
}
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 = Vector3.Lerp(transform.position,targetPosition,80 * Time.deltaTime);
if (transform.position == targetPosition)
return;
Vector3 diff = targetPosition - transform.position;
Vector3 moveDir = diff.normalized * 25 * Time.deltaTime;
if (moveDir.sqrMagnitude < diff.sqrMagnitude)
controller.Move(moveDir);
else
controller.Move(diff);
}
private void FixedUpdate()
{
if (!PlayerManager.isGameStarted)
return;
controller.Move(direction * Time.fixedDeltaTime);
}
private void Jump()
{
direction.y = jumpForce;
}
public void Die ()
{
alive = false;
// Restart the game
Invoke("Restart", 2);
if (!alive) return;
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
if (!alive) return;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.transform.tag == "Obstacle")
{
PlayerManager.gameOver = true;
FindObjectOfType<AudioManager>().PlaySound("GameOver");
FindObjectOfType<AudioManager>().StopSound("Main Theme");
}
}
}
public class TileManager : MonoBehaviour {
public GameObject[] tilePrefabs;
public float zSpawn = 0;
public float tileLength = 50;
public int numberOfTiles = 5;
private List<GameObject> activeTiles = new List<GameObject>();
public Transform playerTransform;
// Start is called before the first frame update
void Start()
{
for(int i= 0; i < numberOfTiles; i++)
{
if (i == 0)
SpawnTile(0);
else
SpawnTile(Random.Range(0, tilePrefabs.Length));
}
}
// Update is called once per frame
void Update()
{
if (playerTransform.position.z -35 > zSpawn - (numberOfTiles * tileLength))
{
SpawnTile(Random.Range(0, tilePrefabs.Length));
DeleteTile();
}
}
public void SpawnTile(int tileIndex)
{
GameObject go =Instantiate(tilePrefabs[tileIndex], transform.forward * zSpawn, transform.rotation);
activeTiles.Add(go);
zSpawn += tileLength;
}
private void DeleteTile()
{
Destroy(activeTiles[0]);
activeTiles.RemoveAt(0);
}
}
Your answer
Follow this Question
Related Questions
how to allow the key to only open 1 door rather than all of them? 0 Answers
(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer
How do I make my projectile deal damage to a target it hit. 1 Answer
Instantiate an object on mouse/cursor position. 1 Answer
My Player is going through the floor after crouching 0 Answers