- Home /
How to make GameObjects not Scale with Camera Zoom?
basically have little square=shaped sprites used for Off Screen Indicators as GameObjects (not GUI). These Sprites are children, designed to indicate where their parent is by pointing to them along the edge of the screen.*
However, when I zoom the camera, these sprites get larger (as does everything else). Is there a way to make a specific GameObject not scale when the camera zooms? (Essentially behave like a UI, but not be a UI).
don't want to implement the indicators as part of the UI, because having them as GameObjects simply works out better for me, in terms of the way I have things arranged.*
My camera is Orthographic, and the whole game is 2D, by the way. (Coded in C#)
Answer by robertbu · Nov 11, 2014 at 05:06 AM
The typical way to solve this problem is to use two cameras. Your UI elements would be on a separate layer with a separate camera. This UI camera would have a higher depth, and the clear flags on the camera would be set to 'Depth Only'.
It can be done by script. The object would be dynamically resize and repositioned to keep it at the same position and size on the screen:
#pragma strict
private var orthoOrg : float;
private var orthoCurr : float;
private var scaleOrg : Vector3;
private var posOrg : Vector3;
function Start() {
orthoOrg = Camera.main.orthographicSize;
orthoCurr = orthoOrg;
scaleOrg = transform.localScale;
posOrg = Camera.main.WorldToViewportPoint(transform.position);
}
function Update() {
var osize = Camera.main.orthographicSize;
if (orthoCurr != osize) {
transform.localScale = scaleOrg * osize / orthoOrg;
orthoCurr = osize;
transform.position = Camera.main.ViewportToWorldPoint(posOrg);
}
}
Works perfectly, thank you very much for your help. Converted it to C#:
using UnityEngine;
using System.Collections;
public class NoScaleGameOBject : $$anonymous$$onoBehaviour {
private float orthoOrg;
private float orthoCurr;
private Vector3 scaleOrg;
private Vector3 posOrg;
void Start() {
orthoOrg = Camera.main.orthographicSize;
orthoCurr = orthoOrg;
scaleOrg = transform.localScale;
posOrg = Camera.main.WorldToViewportPoint(transform.position);
}
void Update() {
var osize = Camera.main.orthographicSize;
if (orthoCurr != osize) {
transform.localScale = scaleOrg * osize / orthoOrg;
orthoCurr = osize;
transform.position = Camera.main.ViewportToWorldPoint(posOrg);
}
}
}
Your answer
Follow this Question
Related Questions
GUI controlling other game objects 0 Answers
How can a camera activate an animation 2 Answers
GameObject Moving Slow when drag with hand 0 Answers
Move gameobject pivot 6 Answers
Keeping a Camera focused on the GameObject in a 2D Game 0 Answers