- Home /
How To Limit A transform MOVEMENT in X axis
I am currently working on a game where Camera movement is around X axis only like angory birds. I am applying transform.translate to move the camera.
My problem is that
minoffset //minimum value of camera x position maxoffset //maximum value of camera x postion I using following code
`if (transform.position.x >= MinOffset && transform.position.x <= MaxOffset) {
do the movement stuff
}`
when I use transform. translate using touch The position of camera moves outside the offset values .and the code written in if satement never executes.
I have also tried with two another conditions to check is x position goes beyond the offset value. Reset its position to maximum value. But It produces a jerk .Please Help
Answer by criptoonita · Dec 19, 2012 at 10:37 PM
Maybe clamp would be more elegant to make limits transform.position = Vector3(Mathf.Clamp(pObject.x, min, max), pObject.y, pObject.z);
http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Clamp.html
Answer by CodeMasterMike · Dec 13, 2012 at 01:58 PM
I think that it jerks because you translate it, check if its out of limits, and if it is out of limits, you move it back to within the limits the next frame.
Better way to do it, is to check the final position, and if that is within your boundaries, then do the translate. Something like this pseudo code:
private Vector3 translateVector = new Vector3(10.0f, 0.0f, 0.0f);
update()
{
Vector3 newPosition = PlayerObject.Transform.Position + translateVector;
if(newPosition.x >= MinOffset && newPosition.x <= MaxOffset)
{
PlayerObject.Transform.Translate(translateVector);
}
}
Good luck!
Thats odd. Can you post the code where you do the translation together with the offset check?
Actually according to you, fist I need to store my new Position in another variable
And then 1st check the new position is less then offset. But I have done my movement stuff inside my Condition. ( if (transform.position.x >= $$anonymous$$inOffset && transform.position.x <= $$anonymous$$axOffset) {})..
But still I like your idea .I will try to change my conditions according to you. Thanks :)
Yes, that is a part of it. But where and how do you set the translation vector? The code above only shows the $$anonymous$$/max check, and nothing more.
It's impossible to see if something is wrong if we can't see the code the problem is about. In my example above, you can see how and where I check the new position and how and where I set it, which makes it possible to see if there is anything that's wrong or should be changed.
Thats perfect I have tried with this float newPosition = myPositionX - (swipe$$anonymous$$ovement *2); if(newPosition > CameraZoomInX$$anonymous$$inPosition && newPosition < CameraZoomInX$$anonymous$$axPosition) { myCameraTransfrom.Translate(-swipe$$anonymous$$ovement *2, 0, 0, Space.Self); }
There is one more problem I am facing is that if I am doing the scroll thing more faster the camera again some times moves outside the $$anonymous$$inOffet /$$anonymous$$axOfset.
Please provide me a more better approach so that it can be strictly restricted to the boundaries..
Answer by iuripujol · Nov 23, 2017 at 12:36 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour { public Transform cube; public float dist2; Vector3 initPos; // Use this for initialization void Start () { initPos = transform.position; dist2 = cube.lossyScale.x / 2; }
// Update is called once per frame
void Update () {
float h = Input.GetAxis ("Horizontal");
float s = 0;
if (h != 0) {
s = h > 0 ? 1 : -1;
}
Vector3 newPos = transform.position + cube.right * s;
if(Mathf.Abs (newPos.x) <= dist2)
transform.Translate (cube.right *s * 0.08f);
}
}