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 Zyrac · Aug 20, 2016 at 11:45 PM · c#gameobjecttransform.positionchange

Any way to change transform.position of an indefinite number of gameobjects on FixedUpdate?

So, I want to implement a feature into my game where if you zoom out, you see translucent images of the game sprites that increase in size (and move as needed so that the distances between objects remain to scale) as the camera orthographic size increases, so that you can still see where they are. It's easy enough to find the gameobjects I need with a foreach loop that searches for gameobjects with a certain sprite. I know that to change the scale and position I need to set them as a new Vector2, something like this:

 gameobject.transform.localscale = new Vector2(scaleX, scaleY);
 gameobject.transform.position = new Vector2(positionX, positionY);


Since all the relevant objects are the same size as each other, the scale isn't a problem either. However, I've thought for ages about how I might do it, but I can't figure out a way of creating a new position Vector2 for each of an indefinite amount of gameobjects. Is it possible or do I simply have to allow for a set amount gameobjects? Thank you! :)

Comment
Add comment · Show 8
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 sbsmith · Aug 21, 2016 at 12:22 AM 1
Share

I think the question could use a bit of clarification. If all change is taking place relative to each other, would it not be simplest to have them parented under the same GameObject and then change the scale and position of that?

avatar image Zyrac · Aug 21, 2016 at 04:59 PM 0
Share

Thanks for the suggestions, everyone! I'll have a look at applying them to my project when I get the chance. :)

avatar image Zyrac · Aug 21, 2016 at 09:03 PM 0
Share

Thanks for all the help, guys. sbsmith your method did work, but sadly my idea didn't work out as I imagined. I'll reward you guys for the advice anyway. :)

avatar image sbsmith Zyrac · Aug 21, 2016 at 09:08 PM 0
Share

What was it that didn't work? I'm still a bit foggy on what you were trying to do, but if you wanted a Google$$anonymous$$aps-style map where you zoom out and the little red tags stay the same size, then that is totally doable. Though, if it gets cluttered you'd need extra logic to consolidate the bunched up tags into some sort of summary tag.

avatar image Zyrac sbsmith · Aug 21, 2016 at 09:45 PM 0
Share

I guess that google maps analogy pretty much sums up what I wanted actually! I've essentially got a network of stations and I want the player to be able to zoom out and still be able to tell where they are.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Aug 21, 2016 at 12:42 AM

you would need a second vector2 array to remember the real positions so you know where they are sopposed to be and i think the idea would be to multiply the positions and scales by the camera height. here is a sloppy untested code idea to get you thinking:

 var cam:GameObject;
     cam = gameObject.Find("Camera Main");
 var dudes:GameObject[];
     dudea = FindObjectsOfType(GameObject) as GameObject[];
 var spots:Vector2[];
     spots = new Vector2[dudes.Length];
 var startscale:float; 
 startscale=dudes[1].transform.localScale.x; 
      
     var numbertoplaywith:float;    
     var i:int;
     i = dudes.Length;
     while(i>0){i--
     spots[i].x=dudes[i].transform.position.x;
     spots[i].y=dudes[i].transform.position.x;
     }
    function Update () {
     i = dudes.Length;
     while(i>0){i--;
     dudes[i].transform.position.x=spots[i].x*cam.transform.position.y*numbertoplaywith;
     dudes[i].transform.position.y=spots[i].y*cam.transform.position.y*numbertoplaywith;
     
     dudes[i].transform.localScale.x=startscale*cam.transform.position.y*numbertoplaywith;
     dudes[i].transform.localScale.y=startscale*cam.transform.position.y*numbertoplaywith;
     }}
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
avatar image
0

Answer by IsaiahKelly · Aug 21, 2016 at 02:22 AM

It sounds like you want to scale certain sprites along with the camera's orthographic size to maintain a constant sprite size on screen while zooming in/out of a map.

Here's a script you can attach to each object that needs to scale with with the camera's orthographic size.

 using UnityEngine;
 
 public class ScaleWithCamera : MonoBehaviour
 {
     private Vector3 startScale;
     private float startOrthoSize;
 
     public void Start()
     {
         startScale = transform.localScale;
         startOrthoSize = Camera.main.orthographicSize / 2;
     }
 
     public void LateUpdate()
     {
         float orthoSize = Camera.main.orthographicSize / 2;
         var xScale = startScale.x / startOrthoSize * orthoSize;
         var yScale = startScale.y / startOrthoSize * orthoSize;
         var zScale = startScale.z / startOrthoSize * orthoSize;
 
         transform.localScale = new Vector3(xScale, yScale, zScale);
     }
 }

You could also use this code in a single for loop that iterates over all objects that need it, but just attaching a script to each object will be the easiest way to go.

Another alternative would be to use a UI Canvas instead, and draw a UI element (image icon) over each object's position when you zoom out. Using one of the techniques found here

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

215 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

Related Questions

Help with C# - destroy at a certain position not working 0 Answers

GameObject.transform.position on the y axis keeps changing even though object is stationary 1 Answer

C# Change Script Help 1 Answer

Change gameobject tag when player collides with another gameobject 1 Answer

Distribute terrain in zones 3 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