I am able to restrict the movements successfully with the following codes. But I wanted to make it suitable for any screen size thus wanted to restrict the movement range according to screen size but it is not working. The objects origin is (0,0,0). Thanks for any advice.
//Working code where I input exact figures
if (transform.position.x <= -6)
{
transform.position = new Vector3(-6, transform.position.y, transform.position.z);
}
if (transform.position.x >= 6)
{
transform.position = new Vector3(6, transform.position.y, transform.position.z);
}
//Not working. I am trying to use screen size here.
if (transform.position.x <= (Screen.width/2) * -1)
{
transform.position = new Vector3((Screen.width/2) * -1, transform.position.y, transform.position.z);
}
if (transform.position.x >= (Screen.width / 2))
{
transform.position = new Vector3((Screen.width / 2), transform.position.y, transform.position.z);
}
you could get a pixel to meter ratio, plug that in and use it to modify a Mathf.Clamp to your needs.
//Understand that this is my tenth Time messing around with this so I'm a little angry
//javascript for future passerbyers
public var holdMyRatio : float;// set to pixel to meter ratio
private var holdMyScreenWidth : float;//set to screen width (this has to be done in awake)
function Awake () {
holdMyScreenWidth = (Screen.Width / 2) * holdMyRatio;
}
function Update () {
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -holdMyRatio, holdMyRatio), transform.position.y, tranform.position.z);
}
//C#
//Insert the usings and class here
public float holdMyRatio;
private float holdMyScreenWidth;
void Awake () {
holdMyScreenWidth = (Screen.Width / 2) * holdMyRatio;
}
void Update () {
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -holdMyRatio, holdMyRatio), transform.position.y, tranform.position.z);
}
you could just use Mathf.Clamp to simplify your code to two lines
The best way to do this is to use ScreenToWorldPoint to take an area ‘seen’ by the camera and turn it into a 3D world point.
With an orthographic camera this is simple because depth becomes a non issue.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
private Vector3 topLeftBoundary = Vector3.zero; //Will hold the topleft corner of the screen position.
private Vector3 bottomRightBoundary = Vector3.zero; //Holds the bottom right corner.
// Use this for initialization
void Start () {
//Set the boundary information to the actual screen positions. 0, 0 is top left corner.
topLeftBoundary = camera.ScreenToWorldPoint(new Vector3(0, 0, camera.farClipPlane));
bottomrightboundary = camaera.screenToWorldPoint(new Vector3(Screen.width, Screen.height, camera.farClipPlane));
}
// Update is called once per frame
void Update () {
}
}
However, if you want a perspective camera you’ll need to change “camera.farClipPlane” to the z-depth of the point in relation to the camera. You’ll want to pull it to the location of the player so something like player.transform.z would go in there instead.