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 /
This question was closed Aug 03, 2020 at 06:39 AM by surajbhan for the following reason:

I solved it.

avatar image
0
Question by surajbhan · Jul 30, 2020 at 04:44 AM · transform.positionaxispositioning

Please help! I am new to unity and coding. I am trying to set my level at axis x =10.75148 and axis z= 3 but when script runs x and z axis becomes negative

I am trying to set my level at axis x =10.75148 and axis z= 3 but when script runs x and z axis becomes negative. I am trying to make infinity levels on y axis. On Y axis it seems fine but x axis and z axis becomes negative once level parent object TileManager script runs. Tile manager position sets as axis x =10.75148 , axis Y = 0 and axis z= 3

Please kindly help someone.

Below is my Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TileManager : MonoBehaviour
 {
     // Start is called before the first frame update
 
     public GameObject[] tilePrefabs;
 
     private Transform playerTransform;
 
     private float spawnY = 115.9884f;
     private float tileLenght = 499.8616f;
     private float spawnX = 10.75148f;
     private float spawnZ = 3f;
 
     private int AmtOfTiles = 3;
     private List<GameObject> activeTiles;
     private int LastPrefabIndex = 0;
 
     private void Start()
     {
         activeTiles = new List<GameObject>();
         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
 
         for (int i = 0; i < AmtOfTiles; i++)
         {
             if (i < 2)
                 SpawnTile(0);
             else
                 SpawnTile();
         }
     }
 
     // Update is called once per frame
     private void Update()
     {
         if (playerTransform.position.y > (spawnY - AmtOfTiles * tileLenght))
         {
             SpawnTile();
             DeleteTile();
         }
 
     }
 
     private void SpawnTile(int prefabIndex = -1)
     {
         GameObject levelgo;
         if (prefabIndex == -1)
 
             levelgo = Instantiate(tilePrefabs[RandomPrefabIndex()]) as GameObject;
 
         else
             levelgo = Instantiate(tilePrefabs[prefabIndex]) as GameObject;
 
         levelgo.transform.SetParent(transform);
         levelgo.transform.position = Vector3.up * spawnY;
         
 
         spawnY += tileLenght;
       
         activeTiles.Add(levelgo);
     }
 
     private void DeleteTile()
     {
         Destroy(activeTiles[0]);
         activeTiles.RemoveAt(0);
     }
 
     private int RandomPrefabIndex()
 
     {
         if (tilePrefabs.Length <= 1)
             return 0;
         int randomIndex = LastPrefabIndex;
         while (randomIndex == LastPrefabIndex)
         {
             randomIndex = Random.Range(0, tilePrefabs.Length);
         }
         LastPrefabIndex = randomIndex;
         return randomIndex;
     }
 
 }

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

  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Jul 30, 2020 at 08:50 AM

You can always use the Mathf.Abs()function to always get an absolute value which means your numbers will never be negative. E.g: Mathf.Abs((spawnY - AmtOfTiles * tileLenght)) will always return a positive number, instead of a negative. Check your BODMAS too, maybe spawnY - (AmtOfTiles * tileLength) was negative.

Comment
Add comment · Show 1 · 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
avatar image surajbhan · Aug 03, 2020 at 06:38 AM 0
Share

I fixed it. Issue is not with the code. It was about the position of the objects.

Follow this Question

Answers Answers and Comments

133 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

Related Questions

Changing water level 0 Answers

Define position as lower left corner? 0 Answers

Transforming 2D object with respect to UI 1 Answer

Transform.position not working ??? 0 Answers

How do I make and object's position and rotation independent again? 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