• 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
1
Question by TritestMetal · May 05, 2012 at 02:30 PM · transformlocalpositionrunner

Following runner tutorial, cannot understand a particular line

Hi, I am following the catlikecoding runner tutorial and there is a line in the skylinemanager which is really confusing me. I have been following through the code and commenting what I THINK is going on (learning C# - I'm an ASM programmer so still trying to grasp OO concepts...)

The line is

 position.y += scale.y * 0.5f;

and I cannot grasp why the skyline cubes do not rise in the y axis over time as the previous value seems to be summed with the new. I know it does work correctly, because, well.., it does! But I cannot see why.

I have included my commented version of the code below so any helpful person can hopefully see my thought process and where I am presumably misunderstanding the fundamentals.

Many thanks, Chris

 using UnityEngine;    
 using System.Collections.Generic;
 
 public class SkylineManager : MonoBehaviour 
 {
     public Transform prefab;    
     public int numberOfObjects;    
     public float recycleOffset;    
     public Vector3 minSize, maxSize;
 
     private Vector3 nextPosition;    
     private Queue<Transform> objectQueue;
 
     // Use this for initialization    
     void Start () {    
         objectQueue = new Queue<Transform>(numberOfObjects);            // create new queue of transform objects - size numberOfObjects
 
         for (int i = 0; i < numberOfObjects; i++){    
             objectQueue.Enqueue((Transform)Instantiate (prefab));       // instatiate a prefab (defined in unity editor) and add it's transform to queue    
         }
         nextPosition = transform.localPosition;                         // get position of parent transform and store in Vector3 nextPosition
 
         for (int i = 0; i < numberOfObjects; i++){                      // iterate through each object    
             Recycle();                                                  // use recycle to set positions and scale of prefabs    
         }
     }
 
     // Update is called once per frame    
     void Update () {    
         if (objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTravelled ){    //next cube in queue far behind runner?          
             Recycle();    
         }    
     }
    
     void Recycle(){
         Vector3 scale = new Vector3(                                    // create new Vector3 to store scale and randomise value within ranges    
         Random.Range (minSize.x, maxSize.x),            
         Random.Range (minSize.y, maxSize.y),    
         Random.Range (minSize.z, maxSize.z));            
 
         Vector3 position = nextPosition;                                // create new vector3 for position and set to values in nextPosition    
         position.x += scale.x * 0.5f;                                   // increase position.x to last x value + center of next prefab    
         position.y += scale.y * 0.5f;                                   // increase position.y to last y value + center of next prefab             
 
         Transform o = objectQueue.Dequeue();                            // Get oldest prefab transform from queue and assign it's transform to o     
         o.localScale = scale;                                           // set scale of prefab as defined line 40    
         o.localPosition = position;                                     // set localPosition of prefab as defined Line 45
 
         nextPosition.x += scale.x;                                      // update nextPosition to control placement of next o (used next iteration)    
         objectQueue.Enqueue(o);                                         // return prefab transform to Queue    
     }    
 }

[Edit by Berenger, code formatting]

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
1

Answer by aldonaletto · May 05, 2012 at 03:20 PM

If the object is an Unity cube (or any object with original size = 1), the script is using scale as size. nextPosition indicates the "lower left" position for the next object. Since its pivot is at the center, the actual object position is shifted 50% of its size to the left and above, so it will have its lower left corner at nextPosition (as you've presumed in your comments). But notice that nextPosition isn't affected by this shift - only nextPosition.x is incremented by scale.x later to place the next object immediately to the right of this one.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get correct position after RecalculateBounds is called? 1 Answer

Find a gameobject with the same position 2 Answers

Updating localPosition(?) in a scaled transform parent 1 Answer

Multiple Coordinate Systems 1 Answer

Control the speed of movement of object? 2 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