Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 unityshuvo · Apr 29, 2020 at 05:25 AM · 3d

How to add player on road spawn script

Hiii. I am a beginner game developer with no coding experience. I am making a endless car game. I want add player by selecting them from menu. If I change car from menu the road spawn script won't work. Here's my script I used for road spawn : using UnityEngine; using System.Collections; using System.Collections.Generic;

public class RoadGenerator : MonoBehaviour {

 // road chunk prefab
 public GameObject roadChunk;

 // distance between edges of the chunk.
 public float chunkLength;

 // number of chunks to be activated at a time.
 public int drawingAmount = 3;

 // reference to player object. it is required to manage chunks on the scene.
 [SerializeField]private Transform player = null;

 // total number of chunks that actually exist in the scene
 [SerializeField]private int numberOfChunks = 7;

 // list of references to chunks in the scence
 private Queue<Transform> chunks;

 // reference to chunk that the player is on
 private Transform currentChunk;
 private int indexOfCurrentChunk;
 private int currentChunkPosition = 0;

 private void Awake()
 {
     InitializeChunksList();
 }

 private void InitializeChunksList()
 {
     chunks = new Queue<Transform>();
     for (int i = 0; i < numberOfChunks; i++)
     {
         GameObject _chunk = Instantiate<GameObject>(roadChunk);
         _chunk.transform.position = NextChunkPosition();
         if (i != 0)
             _chunk.SetActive(false);
         chunks.Enqueue(_chunk.transform);
     }
 }
  
 private void Start () {
     
 }
 
 private void FixedUpdate () {

     if (!player) return;

     // determine the chunk that the player is on
     currentChunk = GetCurrentChunk();
     indexOfCurrentChunk = GetIndexOfCurrentChunk();

     // Manage chunks based on current chunk that the player is on
     for (int i = indexOfCurrentChunk; i < (indexOfCurrentChunk+drawingAmount); i++)
     {
         i = Mathf.Clamp(i, 0, chunks.Count-1);
         GameObject _chunkGO = (chunks.ToArray()[i]).gameObject;
         if (!_chunkGO.activeInHierarchy)
             _chunkGO.SetActive(true);
     }

     if (indexOfCurrentChunk > 0)
     {
         float _distance = Vector3.Distance(player.position, (chunks.ToArray()[indexOfCurrentChunk - 1]).position);
         if(_distance > (chunkLength * .75f))
             SweepPreviousChunk();
     }

 }

 private void SweepPreviousChunk()
 {
     Transform _chunk = chunks.Dequeue();
     _chunk.gameObject.SetActive(false);
     _chunk.position = NextChunkPosition();
     chunks.Enqueue(_chunk);
 }

 private Vector3 NextChunkPosition()
 {
     float _position = currentChunkPosition;
     currentChunkPosition += (int)chunkLength;
     return new Vector3(0, 0, _position);
 } 

 private Transform GetCurrentChunk()
 {
     Transform current_chunk = null;
     foreach (Transform c in chunks)
     {
         if (Vector3.Distance(player.position, c.position) <= (chunkLength / 2))
         {
             current_chunk = c;
             break;
         }
     }
     return current_chunk;
 }

 private int GetIndexOfCurrentChunk()
 {
     int index = -1;
     for (int i = 0; i < chunks.Count; i++)
     {
         if ((chunks.ToArray()[i]).Equals(currentChunk))
         {
             index = i;
             break;
         }
     }
     return index;
 }

}

And sorry for my spelling..

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by iveL_newO · Apr 29, 2020 at 08:06 AM

It looks like your script is referencing your player's transform. Depending on how you switch cars that might not be updated when you switch.

If you are instantiating a new player object then your script won't be able to find its transform because it will still be looking at the old cars transform. You could fix this by changing your "player" transform if you switch cars.

Comment
Add comment · Share
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

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

164 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

Related Questions

GUI Follow RaycastHit 2 Answers

How to make a 3d object act like GUI? 1 Answer

Damage script not working? 1 Answer

Football penalty game- Move gameobject in direction of swipe with force 0 Answers

Character won't jump script help. 1 Answer


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