- Home /
Restrict movement along x-axis
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 are mixing screen coordinates with world coordinates. You need to do all of your calculations in world coordinates. Is this a perspective camera or an orthographic camera?
This is for orthographic camera. Would like to know about perspective too for knowledge sake if possible. :)
Answer by JNetRocks1316 · Jan 02, 2014 at 04:06 AM
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.
Answer by TheShadowblast123 · Jan 02, 2014 at 02:57 AM
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
Hi, I don't understand the portion where I set pixel to meter ratio. How do I calculate this value?
Also for the Awake method, I don't get what is going on there. It is just a variable being assigned a value. I don't get what is the action that is happening there due to this assignment.
Sorry if noobish questions.
$$anonymous$$athf.Clamp is not working for me even if I use exact values ins$$anonymous$$d of screen size as follows:
Vector3 pos = transform.position; pos.x = $$anonymous$$athf.Clamp(transform.position.x, -6,6);
Answer by sath · Jan 02, 2014 at 03:24 PM
is this what u need?
http://answers.unity3d.com/questions/62189/detect-edge-of-screen.html