Calling my background Sprite to fill screen in Unity - unity3d

I know that there are many questions like this on the net but I haven't found a solution to my problem yet...
I'm trying to make a background image that fills the screen. In my game, there is a canvas and inside this Canvas there is a gameObject which has attached the sprite component.
Many solutions includes this code:
void Awake()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
double variable = Camera.main.orthographicSize * 4.0;
float worldScreenHeight = (float)variable;
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
transform.localScale = new Vector2(worldScreenWidth / width, worldScreenHeight / height);
}
But I tried it and it makes the gameobject really small.
Any ideas?

At first, select your Canvas game object. in Canvas Scaler component set UI Scale Mode to Constant Pixel Size.
Note that, when you set UI Scale Mode to Scale With Screen Size, your image changes by screen width or height.
Also, Note that your image should be in the center of its parent. Set the transform position and rotation of your image to this:
Now Change your code to this:
void Awake()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
transform.localScale = new Vector2(Screen.width / width, Screen.height / height);
}
Hope it helps you.

Related

How to make camera follow to clone player not first production?

I made the code which can follow the clone player in unity. But It worked only the first clone player. If join room second or later, It can't find clone player and doesn't work. How can I fix that ? Thanks for anything your helps.
follow.cs:
private void Awake()
{
mainCam = this;
}
void Start()
{
height = Camera.main.orthographicSize;
width = height * Screen.width / Screen.height;
}
void LateUpdate()
{
if (player == null)
{
player = GameObject.Find("Player(Clone)");
}
else
{
//transform.position = new Vector3(target.position.x, target.position.y, -10f);
transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * speed);
float lx = size.x * 0.5f - width;
float clampX = Mathf.Clamp(transform.position.x, -lx + center.x, lx + center.x);
float ly = size.y * 0.5f - height;
float clampY = Mathf.Clamp(transform.position.y, -ly + center.y, ly + center.y);
transform.position = new Vector3(clampX, clampY, -10f);
}
}
player spawn script:
public void Spawn()
{
GameObject PI = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity);
Follow.mainCam.target = PI.transform;
Debug.Log("I'm in the room.");
}
I guess that player stay null, maybe when you are trying to GameObject.Find() by his name, that method returns null.
Maybe you can try it with a tag or use another name?
The simplest solution is to make the camera the child of the player object and then change it's position accordingly through the script
You could also potentially use CineMachine which has some built-in follow methods
In order to change the parent of an object in the hierarchy through code you basically need to do something like this
public Transform camera
public Transform player
camera.parent = player;
and you can then set the camera position like this
camera.position = new Vector3(0, 0, -10);
Since the camera is the child of the player 0, 0, -10 basically means that the camera will be -10 in the Z axis away from the player (which should be behind the player). Whenever the player moves, because the camera is now attached as a child, it will follow the player.

how to position a UI Canvas at the same point as it's parent game object

I made a UI Canvas prefab which has an empty game object with the following script attached to it:
float remainderAngle;
Image rotAngleWedge;
Image remAngleWedge;
public Image rotateWedgePrefab;
float zRotation = 0f;
public void PieGraph (float ringRotateAngle, Transform parentTransform) {
remainderAngle = 360f - ringRotateAngle;
rotAngleWedge = Instantiate (rotateWedgePrefab) as Image;
rotAngleWedge.transform.position = parentTransform.position;
rotAngleWedge.transform.SetParent (transform, false);
rotAngleWedge.color = Color.yellow;
rotAngleWedge.fillAmount = ringRotateAngle / 360f;
zRotation = Quaternion.Euler (parentTransform.eulerAngles).z - 45.0f;
rotAngleWedge.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, zRotation));
zRotation -= rotAngleWedge.fillAmount * 360f;
remAngleWedge = Instantiate (rotateWedgePrefab) as Image;
remAngleWedge.transform.position = parentTransform.position;
remAngleWedge.transform.SetParent (transform, false);
remAngleWedge.color = Color.white;
remAngleWedge.fillAmount = remainderAngle / 360f;
remAngleWedge.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, zRotation));
}
Then I called the PieGraph method in the parent game object script after creating the prefab using. I can't seem to get the UI Canvas to be positioned at the same place as its parent object. Please see images of the inspector settings for the Canvas and its child object below. How can I position them at the same place?
UI Canvas Prefab Inspector Settings
UI Canvas Prefab Child Game Object Inspector Settings
You can try to assign RenderMode to world space.

Make rectangle in Unity

I stuck on the basics of Unity. I want to make a scene for mobiles in which there are four different coloured rectangles that take 25% of the screen each.
I tried making an GameObject Image, "registering" it as a prefab in Inspector.
Below code is an example of how I tried to make a single red rectangle and position it on the (x,y,z) => (0,0,0) coordinates on my scene.
Several problems are present:
Rectangle did not appear
I don't know how to programatically specify width and height of the rectangle
This is how it looks:
public class SceneScript : MonoBehaviour {
public GameObject prefab;
void Start () {
Vector3 pos = new Vector3(0, 0, 0);
GameObject gameObject = Instantiate(prefab);
Image image = gameObject.GetComponent<Image>();
image.color = new Color(1.0F, 0.0F, 0.0F);
gameObject.transform.position = new Vector3(0, 0, 0);
}
// Update is called once per frame
void Update () {
}
}
Is there an easier solution, or is this the best practice + could you please provide me some hints what should I do?
You probably don't have a Canvas in your heirarchy. Here's how you can programmatically create your Canvas, your Image and their containing GameObjects:
Vector3 pos = new Vector3(0, 0, 0);
GameObject parentGameObject = new GameObject();
Canvas canvas = parentGameObject.AddComponent<Canvas>();
GameObject imageGameObject = new GameObject();
imageGameObject.transform.SetParent(canvas.transform);
Image image = imageGameObject.AddComponent<Image>();
image.color = new Color(1.0F, 0.0F, 0.0F);
imageGameObject.transform.position = pos;
This will create a full-screen, red rectangle. Play around with the RectTransform settings in the inspector after these are created and you should be able to figure out how to size them properly.

unity3d (2d!) - Camera to centre on player, but never exceed "map" bounds

I am creating a game which has up to 4 orthographic cameras (for up to 4 players), which take up various amounts of the screen, depending on the amount of players.
I have a script which controls all the cameras, setting their position relative to the players they are watching.
What I want to achieve is a simple overhead-runner style of movement, whereby the camera will follow the player, but not go outside the bounds of the map.
I have managed to get the boundaries working in the top-left camera, when the cameras are 'square' (as in the 4-player layout). However, the other cameras don't track properly at all, and in the rectangular 2-player mode, the top camera still goes too far left-and right. I'm pretty sure I know exactly which line of code is causing the problem... but I don't know what I need to do to fix it...
SpriteRenderer spriteBounds = GameObject.Find("Map").GetComponentInChildren<SpriteRenderer>();
float leftBound =0;
float rightBound =0;
float bottomBound =0;
float topBound = 0;
if((trackPlayer1 == null))
{
camPlayer1.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer1.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this??
leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer1.transform.position = new Vector3(Mathf.Clamp(trackPlayer1.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer1.transform.position.y, bottomBound, topBound), camPlayer1.transform.position.z);
}
if((trackPlayer2 == null))
{
camPlayer2.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer2.orthographicSize ;
float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this??
leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer2.transform.position = new Vector3(Mathf.Clamp(trackPlayer2.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer2.transform.position.y, topBound, bottomBound), camPlayer2.transform.position.z);
}
if((trackPlayer3 == null))
{
camPlayer3.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer3.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this??
leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer3.transform.position = new Vector3(Mathf.Clamp(trackPlayer3.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer3.transform.position.y, topBound, bottomBound), camPlayer3.transform.position.z);
}
if((trackPlayer4 == null))
{
camPlayer4.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer4.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this??
leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer4.transform.position = new Vector3(Mathf.Clamp(trackPlayer4.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer4.transform.position.y, topBound, bottomBound), camPlayer4.transform.position.z);
}
So I'm pretty sure that I need to be checking against the cameras size and relative position on the screen, but I am lost as to exactly what I need to be doing.
(edit)
Script explanation:
The script is a global script attached to the main camera object, which is never seen by the player
The four player cams (camPlayer1 - camPlayer4) are public variables and assigned to the script in the designer
trackPlayer1-trackPlayer4 are public gameobjects, which are assigned in the designer - they are assigned to the player objects
Player tracking works on all cams... for example if I change camPlayer2.transform.position = new Vector3(Mathf.Clamp(trackPlayer2.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer2.transform.position.y, topBound, bottomBound), camPlayer2.transform.position.z); to camPlayer2.transform.position = trackPlayer2.transform.position;, the code has the expected effect, the camera follows the player. It is only the clamping to the bounds of the map that I am having issues with
The camera's orthographic sizes are set to 2
code which positions the cameras on screen at startup:
switch (playerCount)
{
case 1:
camPlayer1.enabled = true;
camPlayer2.enabled = false;
camPlayer3.enabled = false;
camPlayer4.enabled = false;
camPlayer1.rect = new Rect(0, 0, 1, 1);
camPlayer1.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer2.rect = new Rect(0, 0, 0, 0);
camPlayer2.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer3.rect = new Rect(0, 0, 0, 0);
camPlayer3.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer4.rect = new Rect(0, 0, 0, 0);
camPlayer4.orthographicSize = CamManager.CAMERA_SIZE;
break;
case 2:
camPlayer1.enabled = true;
camPlayer2.enabled = true;
camPlayer3.enabled = false;
camPlayer4.enabled = false;
camPlayer1.rect = new Rect(0, 0.5f, 0.7f, 0.5f);
camPlayer1.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer2.rect = new Rect(0.3f, 0, 0.7f, 0.5f);
camPlayer2.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer3.rect = new Rect(0, 0, 0, 0);
camPlayer3.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer4.rect = new Rect(0, 0, 0, 0);
camPlayer4.orthographicSize = CamManager.CAMERA_SIZE;
Destroy(play3);
Destroy(play4);
break;
case 3:
camPlayer1.enabled = true;
camPlayer2.enabled = true;
camPlayer3.enabled = true;
camPlayer4.enabled = false;
camPlayer1.rect = new Rect(0, 0.5f, 0.5f, 0.5f);
camPlayer1.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer2.rect = new Rect(0.5f, 0.5f, 0.5f, 0.5f);
camPlayer2.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer3.rect = new Rect(0.25f, 0, 0.5f, 0.5f);
camPlayer3.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer4.rect = new Rect(0, 0, 0, 0);
camPlayer4.orthographicSize = CamManager.CAMERA_SIZE;
Destroy(play4);
break;
case 4:
camPlayer1.enabled = true;
camPlayer2.enabled = true;
camPlayer3.enabled = true;
camPlayer4.enabled = true;
camPlayer1.rect = new Rect(0, 0.5f, 0.5f, 0.5f);
camPlayer1.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer2.rect = new Rect(0.5f, 0.5f, 0.5f, 0.5f);
camPlayer2.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer3.rect = new Rect(0, 0, 0.5f, 0.5f);
camPlayer3.orthographicSize = CamManager.CAMERA_SIZE;
camPlayer4.rect = new Rect(0.5f, 0, 0.5f, 0.5f);
camPlayer4.orthographicSize = CamManager.CAMERA_SIZE;
break;
}
}
edit, so I can get the first camera to tract regardless of shape (so in 2-player mode, with a rectangular camera, the player-1 cam will respect the boundaries of the map) with the code below. I'm guessing then that I need to apply some offsets to the leftBound, rightBound, topBound and bottomBound dependent on the rects of the other cams. How to establish and calculate these I have no idea
if((trackPlayer1 == null))
{
camPlayer1.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer1.orthographicSize;
float horzExtent = vertExtent * (Screen.width * (camPlayer1.rect.width * 2)) / Screen.height; //I guess the problem is here... but how do I fix this??
There are several issues with your calculations. I guess it's just luck that it works for some situations. While your general idea is correct, you're calculating with the wrong properties. Basically you need to understand the aspect ratio of the cameras, the bounding box of your map and how a camera should behave at the borders.
We need the aspect ratio of the camera to calculate its width or extent.
As you can see in the picture, Screen.width and Screen.height are referring to the game's window or the monitor resolution in case you're running the game fullscreen. You can also see that the cameras may have a different aspect ratio when compared to the game window. The good news is that Unity provides a property to get the camera's aspect ratio.
float camVertExtent = cam.orthograpicSize;
float camHorzExtent = cam.aspect * camVertExtent;
Now that we have the camera's extents, let's take a look at your map and its bounding box.
You tried calculating with bounds.size, but as you can see bounds.size.x is the width of the bounding box. Using the size will only work if your map's bottom-left starts at (0,0) in world space. A better approach is to use bounds.min and bounds.max which already return coordinates in world space.
Your ultimate goal is that the camera should stay within the bounds of the map, i.e., its position is restricted by the following four conditions:
(cam.transform.position.x - camHorzExtent) >= bounds.min.x // left
(cam.transform.position.x + camHorzExtent) <= bounds.max.x // right
(cam.transform.position.y - camVertExtent) >= bounds.min.y // bottom
(cam.transform.position.y + camVertExtent) <= bounds.max.y // top
Now you only need to take the player position and limit the camera to these conditions:
float leftBound = bounds.min.x + camHorzExtent;
float rightBound = bounds.max.x - camHorzExtent;
float bottomBound = bounds.min.y + camVertExtent;
float topBound = bounds.max.y - camVertExtent;
float camX = Mathf.Clamp(player.transform.position.x, leftBound, rightBound);
float camY = Mathf.Clamp(player.transform.position.y, bottomBound, topBound);
cam.transform.position = new Vector3(camX, camY, cam.transform.position.z);
If all cameras have the same size and aspect ratio, you can use the same bounds for all player cameras and only calculate camX and camY for each player.
I'd like to suggest you another approach for your problem.
Is it possible to you to add high colliders at the border of your map, and a collider for the camera? You can then make the camera follow the player (use physics!), and when the player reaches a boundary, the camera won't go over, because of the collider.
Then, you can easily split the screen knowing how many players you have.
One player? --> all screen size
Two players? --> camera 1 ends ad screen height / 2, camera 2 starts at screen heigth /2 +1
and so on
Hi everyone for some people that need the complete code for work into game object with many childrens sprites into Empty Gameobject like:
Background
--mountains1(sprite)
--mountains2(sprite)
--mountains3(sprite)
This Code will be loop into Background object and take the bound of all childrens sprites :D, I make a Frankestein code hahhaa thanks to #Stefan Hoffmann explanation. I hope this will be helpful to others, Sorry for my bad English :(
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
private float rightBound;
private float leftBound;
private float topBound;
private float bottomBound;
private Vector3 pos;
private Transform target;
private Camera cam;
private Bounds bounds;
// Use this for initialization
void Start()
{
target = GameObject.FindWithTag("Player").transform;
foreach (SpriteRenderer spriteBounds in GameObject.Find("Background").GetComponentsInChildren<SpriteRenderer>())
{
bounds.Encapsulate(spriteBounds.bounds);
}
cam = this.gameObject.GetComponent<Camera>();
float camVertExtent = cam.orthographicSize;
float camHorzExtent = cam.aspect * camVertExtent;
Debug.Log(camVertExtent);
Debug.Log(camHorzExtent);
Debug.Log(cam.aspect);
Debug.Log(cam.orthographicSize);
leftBound = bounds.min.x + camHorzExtent;
rightBound = bounds.max.x - camHorzExtent;
bottomBound = bounds.min.y + camVertExtent;
topBound = bounds.max.y - camVertExtent;
Debug.Log("leftBound=" + leftBound);
Debug.Log("rightBound=" + rightBound);
Debug.Log("bottomBound=" + bottomBound);
Debug.Log("topBound=" + topBound);
}
// Update is called once per frame
void Update()
{
float camX = Mathf.Clamp(target.transform.position.x, leftBound, rightBound);
float camY = Mathf.Clamp(target.transform.position.y, bottomBound, topBound);
cam.transform.position = new Vector3(camX, camY, cam.transform.position.z);
}
}
You should use this this simple yet useful unity 2d camera follow script
also available on github.
https://gist.github.com/unity3diy/5aa0b098cb06b3ccbe47
using UnityEngine;
using System.Collections;
public class FollowCamera : MonoBehaviour {
public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;
// Use this for initialization
void Start () {
targetPos = transform.position;
}
// Update is called once per frame
void FixedUpdate () {
if (target)
{
Vector3 posNoZ = transform.position;
posNoZ.z = target.transform.position.z;
Vector3 targetDirection = (target.transform.position - posNoZ);
interpVelocity = targetDirection.magnitude * 5f;
targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime);
transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);
}
}
}

Getting the width of a sprite

I am trying to create a row out of some square sprites I have. So to get the width of these sprites i am using
tileWidth = (int)tileSet[0].renderer.bounds.size.x;
And then to form the row i am uisng
for(int i = 0; i < tileSet.Length ; i++){
if((i+1)*tileWidth<screenWidth){
tileSet[i].transform.position = new Vector3(i*tileWidth,0,0);
}
}
But the sprites are still overlapping each other and do not form a proper row.
What am i doing wrong here and how can i rectify it?
If you are using Unity 5 you should use this code:
float tileWidth = tileSet[0].GetComponent<SpriteRenderer>().bounds.size.x;
Pay attention to your pixels per unit.
If the sprite's res is 128x128 pixels.
And this sprite's Pixels To Units is 100.
So your tileWidth will be: renderer.bounds.size.x = 128/100 = 1.28
But you use int: (int)renderer.bounds.size.x = (int)1.28 = 1
and this is why your sprites overlapping each other.
float tileWidth = (float)tileSet[0].renderer.bounds.size.x;
SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
//size in Units
Vector3 itemSize = spriteRenderer.bounds.size;
float pixelsPerUnit = spriteRenderer.sprite.pixelsPerUnit;
itemSize.y *= pixelsPerUnit;
itemSize.x *= pixelsPerUnit;
As jjxtra noted, Verv's answer is not handling rotation properly (and neither is MBehtemam's, as it's the same answer with a slight syntax update).
The following extension method correctly returns the pixel size of a given texture for different orthographic camera sizes, scales, rotations and textures.
public static Vector2 GetPixelSize(this SpriteRenderer spriteRenderer, Camera camera = null)
{
if (spriteRenderer == null) return Vector2.zero;
if (spriteRenderer.sprite == null) return Vector2.zero;
float pixelsPerUnit = spriteRenderer.sprite.pixelsPerUnit;
// Get top left corner
float offsetRight = spriteRenderer.sprite.rect.size.x / 2f / pixelsPerUnit;
float offsetUp = spriteRenderer.sprite.rect.size.y / 2f / pixelsPerUnit;
Vector2 localRight = Vector2.right * offsetRight;
Vector2 localUp = Vector2.up * offsetUp;
// Go to world
Vector2 worldRight = spriteRenderer.transform.TransformPoint(localRight);
Vector2 worldUp = spriteRenderer.transform.TransformPoint(localUp);
Vector2 worldCenter = spriteRenderer.transform.position;
// Go to pixels
Vector2 coordsRight = GetPixelCoordinates(worldRight, camera);
Vector2 coordsUp = GetPixelCoordinates(worldUp, camera);
Vector2 coordsCenter = GetPixelCoordinates(worldCenter, camera);
// Get sizes
float pixelsRight = Vector2.Distance(coordsCenter, coordsRight);
float pixelsUp = Vector2.Distance(coordsCenter, coordsUp);
Vector2 itemSize = Vector2.right * pixelsRight * 2 + Vector2.up * pixelsUp * 2;
return itemSize;
}
public static Vector2 GetPixelCoordinates(this Transform transform, Camera camera = null)
{
if (transform == null) return Vector2.zero;
return GetPixelCoordinates(transform.position, camera);
}
private static Vector2 GetPixelCoordinates(Vector3 position, Camera camera)
{
if (camera == null)
camera = Camera.main;
if (camera == null) return Vector2.zero;
return camera.WorldToScreenPoint(position);
}