- Home /
The question is answered, right answer was accepted
How to spawn prefabs by xMin and xMax position which will change the xmin and xmax position by it self when the screen size is changed?
Hello
i attached the below script to an empty gameobject for spawning prefabs it works fine but the only problem is that i don't know how to set xMin and xMax position that will be able to change by it self xMin and xMax position when the screen size is changed.
Note: some prefabs are having circleCollider, and some of them BoxCollider.
thank you in advance!!!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SpawningColors : MonoBehaviour
{
public GameObject[] ColorSprites;
public float delayTimer = 0.5f;
public float xMaxPosition, xMinPosition;
private float timer;
private int colorObjectNumbers;
// Use this for initialization
void Start ()
{
timer = Time.deltaTime;
}
// Update is called once per frame
void FixedUpdate ()
{
Spawning ();
}
public void Spawning ()
{
timer = timer - Time.deltaTime;
if (timer <=0)
{
Vector3 scale = ColorSprites[colorObjectNumbers].transform.localScale;
float rndmScale = Random.Range (0.8f, 1f);
scale = new Vector3 (rndmScale, rndmScale, rndmScale);
ColorSprites[colorObjectNumbers].transform.localScale = scale; // assign the new scale.
Vector3 colorObjectPos = new Vector3 (Random.Range(xMinPosition, xMaxPosition),transform.position.y, transform.position.z );
colorObjectNumbers = Random.Range(0, 24);
Instantiate(ColorSprites[colorObjectNumbers], colorObjectPos, transform.rotation);
timer = delayTimer;
}
}
}
Answer by AGC · Apr 03, 2016 at 04:04 PM
I find the solution by finding the left and right edges of camera and it works good
here is the updated code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SpawningColors : MonoBehaviour
{
public GameObject[] ColorSprites;
public float delayTimer = 0.5f;
public float xMaxPosition, xMinPosition;
private float timer;
private int colorObjectNumbers;
// Use this for initialization
void Start ()
{
float camDistance = Vector3.Distance(transform.position, Camera.main.transform.position);
rightBorder = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, camDistance)).x;
leftBorder = clampCamera.ViewportToWorldPoint(new Vector3(1, 0, camDistance)).x;
timer = Time.deltaTime;
}
// Update is called once per frame
void FixedUpdate ()
{
Spawning ();
}
public void Spawning ()
{
timer = timer - Time.deltaTime;
if (timer <=0)
{
Vector3 scale = ColorSprites[colorObjectNumbers].transform.localScale;
float rndmScale = Random.Range (0.8f, 1f);
scale = new Vector3 (rndmScale, rndmScale, rndmScale);
ColorSprites[colorObjectNumbers].transform.localScale = scale; // assign the new scale.
Vector3 colorObjectPos = new Vector3 (Random.Range(leftBorder,
rightBorder),transform.position.y, transform.position.z );
colorObjectNumbers = Random.Range(0, 24);
Instantiate(ColorSprites[colorObjectNumbers], colorObjectPos, transform.rotation);
timer = delayTimer;
}
}
}
Follow this Question
Related Questions
Camera Fit Issues on 10 inch tablet 0 Answers
2D snake game stop sprite from leaving screen 1 Answer
Screen.width/Screen.height not working with multiple resolutions 0 Answers
Unity 2D How to Scale or Re-position sprite relative to different Screen sizes 1 Answer
How to adjust positions of the sprites to different screen factors? 2 Answers