- Home /
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! :)
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?
Thanks for the suggestions, everyone! I'll have a look at applying them to my project when I get the chance. :)
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. :)
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.
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.
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;
}}
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
Your answer
Follow this Question
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