- Home /
Camera view level
Ive attached an image, because its hard to explain what I want.
What I want to acheive is: The camera should follow the lightblue color. Right now the camera is following the player, and thats all good, but some places in the game, I want the camera not to follow. How to do that? ?
the camera will be facing hole level you can't poisiton it in 2d games like this
Sorry i have no idea what you trying to ask here. I can tell you want i see here: I see a 2d level layout with platforms, killplanes and i guess the cyan area should be the area where the player should be able to move? What do you actually want to achieve? Do you want to restrict the camera during gameplay? Do you want to render such an image as sort of overview?
This is not a valid question. Please clarify by editing your question.
Ive edited my question. Hope it makes sense now.
Answer by Harinezumi · Apr 17, 2018 at 08:06 AM
If I understand correctly, you would like the camera to follow the player, but restricted to the area marked light blue. If this is correct, then what I would do is to use BoxColliders marked trigger (possibly on their own layer), with scripts on them defining limits, and the camera with a script that looks for the script that defines the limits, and applies it (I once wrote such a system, and it's not difficult, but not trivial either).
Something along these lines:
// put this on your objects that represent the camera movement limits
public class BoxLimit : MonoBehaviour {
[SerializeField] private Vector3 extents; // the size of the limits
public Vector3 GetExtents() { return extents; }
// this helps visualize the limits
private void OnDrawGizmosSelected() {
Gizmos.color = Color.cyan;
Gizmos.DrawWireCube(transform.position, extents);
}
}
// put this on your camera
public class MovementLimiter : MonoBehaviour {
// it's even better if you use a List<BoxLimit> in case there are overlapping areas
private BoxLimit currentLimit;
private void OnTriggerEnter (Collider other) {
BoxLimit boxLimit = other.GetComponent<BoxLimit>();
if (boxLimit != null) {
currentLimit = boxLimit;
}
}
private void OnTriggerExit (Collider other) {
BoxLimit boxLimit = other.GetComponent<BoxLimit>();
if (boxLimit != null && currentLimit == boxLimit) {
currentLimit = null;
}
}
// using LateUpdate() to limit position after all movement is done
private void LateUpdate () {
if (currentLimit != null) {
// get limit variables
Vector3 limitPosition = currentLimit.transform.position;
Vector3 limitExtents = currentLimit.GetExtents() * 0.5f; // halved, because we consider distance from center, not whole scale
// calculate limits
Vector3 lowLimit = limitPosition - limitExtents;
Vector3 highLimit = limitPosition + limitExtents;
// limit position
Vector3 position = transform.position;
position.x = Mathf.Clamp(position.x, lowLimit.x, highLimit.x);
position.y = Mathf.Clamp(position.y, lowLimit.y, highLimit.y);
position.z = Mathf.Clamp(position.z, lowLimit.z, highLimit.z);
// apply position
transform.position = position;
}
}
}
Okay thanks. Yes thats correct, im looking something like that. I will try your suggestion
Can I make multiply cameras, and give each camera there own position and areas?
You can have multiple cameras (in fact, this is about positions, there is nothing about cameras!), but you would need some way to deter$$anonymous$$e which area belongs to which camera.
One simple way is to add a readable int areaId
to BoxLimit
and $$anonymous$$ovementLimiter
, and in $$anonymous$$ovementLimiter.OnTriggerEnter()
only assign it as currentLimit
if the area IDs match.
// add these to both classes, although $$anonymous$$ovementLimiter does not necessarily need the getter
[SerializeField] private int areaId;
public int GetAreaId () { return areaId; }
// in $$anonymous$$ovementLimiter.OnTriggerEnter()
BoxLimit boxLimit = ...;
if (boxLimit != null && boxLimit.GetAreaId() == areaId) {
...
}
Answer by GMAGames · Apr 16, 2018 at 09:27 PM
you can't in 2d mode
Are you serious? There's almost nothing that can't be achieved, though i highly doubt we actually know what he's asking here.
Sorry i was thinking that he want to position the camera in 2d mode like the picture says