Answers for "unity prevent object from leaving camera view"

C#
0

unity prevent object from leaving camera view

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);     
  } 
}
Posted by: Guest on February-12-2020

C# Answers by Framework

Browse Popular Code Answers by Language