Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lighting2O2O · Apr 08, 2021 at 04:53 PM · scripting problemscript.scripting beginnerscritping

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

237 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges