Day: April 29, 2015
-
Making reusable functions in Unity
One of the core fundamentals of programming. Here’s an example: public void randomX(GameObject obj){ GameObject wall = GameObject.Find (“wallLeft”); Vector2 fullScreen = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width,Screen.height)); //world point calculations float wallWidth = wall.GetComponent<Renderer> ().bounds.size.x; float objWidth = obj.GetComponent<Renderer> ().bounds.size.x; float widthRange = fullScreen.x – wallWidth-objWidth/2; float ranX = Random.Range (-widthRange, widthRange); obj.transform.position = new Vector2 (ranX, obj.transform.position.y); } This function gives a game object a random x position on the screen. It takes into consideration padding on the sides (in this case, a wall on both sides) as well as the width of…