How to keep an object within the camera view?

Hello Unity Community!

I am currently working on a 3D space combat game (chicken invaders styled). I would like to know how to always keep an object within the camera view, regardless of resolution. Do I need to change the camera’s field of view according to each resolution? I am using a perspective camera. Should I switch to an orthographic camera instead?

Thank you very much!

-Ashky

This will keep the pivot point a game object visible:

using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {

	void Update() {
		Vector3 pos = Camera.main.WorldToViewportPoint (transform.position);
		pos.x = Mathf.Clamp01(pos.x);
		pos.y = Mathf.Clamp01(pos.y);
		transform.position = Camera.main.ViewportToWorldPoint(pos);
	}
}

You can clamp it tighter if needed. Example:

pos.x = Mathf.Clamp(pos.x, 0.1, 0.9);

Viewport coordinates start at (0,0) in the lower left of the screen and go to (1,1) in the upper right. This code forces an object to have a viewport coordinate in the (0,0) to (1,1) range, and therefore to be on the screen.

I assume you want the object to move independent of the screen aspect ratio:

using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;


public class Controller : MonoBehaviour
{
    public float speed = 0.0f;

    // The clamp margins
    public float
        clampMarginMinX = 0.0f,
        clampMarginMaxX = 0.0f,
        clampMarginMinY = 0.0f,
        clampMarginMaxY = 0.0f;

    // The minimum and maximum values which the object can go
    private float
        m_clampMinX,
        m_clampMaxX,
        m_clampMinY,
        m_clampMaxY;

    private void Start()
    {
        // Get the minimum and maximum position values according to the screen size represented by the main camera.
        m_clampMinX = Camera.main.ScreenToWorldPoint(new Vector2(0 + clampMarginMinX, 0)).x;
        m_clampMaxX = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width - clampMarginMaxX, 0)).x;
        m_clampMinY = Camera.main.ScreenToWorldPoint(new Vector2(0, 0 + clampMarginMinY)).y;
        m_clampMaxY = Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height + clampMarginMaxY)).y;
    }

    private void Update()
    {
        Vector3 direction = Vector3.zero;

        // Going left
        if (Input.GetKey(KeyCode.A))
        {
            direction = Vector2.right * -1;
        }

        // Going right
        else if (Input.GetKey(KeyCode.D))
        {
            direction = Vector2.right;
        }

        if (transform.position.x < m_clampMinX)
        {
            // If the object position tries to exceed the left screen bound clamp the min x position to 0.
            // The maximum x position won't be clamped so the object can move to the right.
            direction.x = Mathf.Clamp(direction.x, 0, Mathf.Infinity);
        }

        if (transform.position.x > m_clampMaxX)
        {
            // Same goes here
            direction.x = Mathf.Clamp(direction.x, Mathf.NegativeInfinity, 0);
        }

        transform.position += direction * (Time.deltaTime * speed);
    }

}

Basically the idea is to clamp the movement direction so the object can’t exceed the screen limits.I didn’t wrote the code for up and down movement but basically it’s almost the same.The clamp margins can define the movement square

alt text