Im working on 2.5D top down shooter for iOS and Android and I need to clamp player position to avoid the player leave game screen. My game setup is camera, moving along Z axis at 60 degrees angle and the player is moving along x,z axis.
Is there any way how to overcome this?
Answer by BarkShark
Nov 08, 2011 at 09:10 PM
Alright, I've figured it out, apparently I made a stupid mistake.
This is the new script, I tested it and it works as it should be. Make sure the camera that is rendering the player is the main camera. Else you have to change the script a little bit.
function Update ()
var dist =(transform.position.y - Camera.main.transform.position.y);
var leftLimitation = Camera.main.ViewportToWorldPoint(Vector3(0,0,dist)).x;
var rightLimitation = Camera.main.ViewportToWorldPoint(Vector3(1,0,dist)).x;
var upLimitation = Camera.main.ViewportToWorldPoint(Vector3(0,0,dist)).z;
var downLimitation = Camera.main.ViewportToWorldPoint(Vector3(0,1,dist)).z;
transform.position.x = Mathf.Clamp(transform.position.x,rightLimitation,leftLimitation);
transform.position.z = Mathf.Clamp(transform.position.z,downLimitation,upLimitation);
Image so you can understand the principe:
The principle remains the same:
-First we create a variable dist, this variable calculates the distance between the players y position and the y position of the camera.
Then we create four variables, leftLimitation and so on. Camera.main.ViewportToWorldPoint calculates the cameras borders and converts them to world coordinates so we can restrict the players transform. We do this both for the x and z axis because these are the axis the player moves along.
Then we create a function that restricts the players position both for the x and z axis.
I hope this solves your problem, sorry for my previous wrong answer.
Thank you for your time! I really apprecite it. Also thank you, that you are explaing the code! Your code would work if you have camera at 90 Degrees(2D).But I position camera at 60 degrees to use 3D effect(2.5D). Im not sure if I making myself clear :/
Yes you do, I'll see if I can help you any further tomorrow.
Im really sorry but I don't know a solution to your problem if you use 2.5D.
Thank you anyway!
i've been trying to re-write this in C#
float dist = (transform.position.y - Camera.main.transform.position.y);
float leftLimitation = Camera.main.ViewportToWorldPoint(new Vector3(0,0,dist)).x;
float rightLimitation = Camera.main.ViewportToWorldPoint(new Vector3(1,0,dist)).x;
float upLimitation = Camera.main.ViewportToWorldPoint(new Vector3(0,0,dist)).z;
float downLimitation = Camera.main.ViewportToWorldPoint(new Vector3(0,1,dist)).z;
float tempx = Mathf.Clamp(transform.position.x,rightLimitation,leftLimitation);
float tempz = Mathf.Clamp(transform.position.z,downLimitation,upLimitation);
transform.position = new Vector3 (tempx, transform.position.y, tempz);
however the 3d object just flickers from top right to bottom left weird?!
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
6 People are following this question.
Limit the position of the object, but still keep following the touch? How to use ClampMagnitude properly?
Embed device maps into unity 3D
Is GameObject.FindWithTag Bad for Mobile Platforming?
How to integrate facebook to unity project for android and ios