- Home /
The question is answered, right answer was accepted in the coments
How to limit movement along an axis?
Hi there,
I guess lets get straight down to business and let me explain the question since phrasing it correctly would of made it way too long. :P
I wrote a script that handles a platforms movement only along a single axis.Lets say the x-axis for the sake of argument.Now I've been trying to further constrain this movement only along the positive x-axis so that doesn't matter which way the world has been rotated (the game involves rotatable enviornment) the platform will always move in the same direction.
I tried using Mathf.Clamp
but that hasn't solved the issue.did not work. If you are wondering what the script looks like then you can check it here where I posted it after solving a different problem that I had with it.http://answers.unity3d.com/questions/515128/fixing-the-nullreferenceexception.html
Just to clarify in case it's still unclear.I'd like to add a few changes to my script that would make the platform ignore the negative x-axis and move only along the positive x-axis.
Any tips and advice are welcome.
Cheers(Thank you) in advance.
You mean this:
if(destinationPos < targetPos && transform.position.z >= 0)
{
transform.position.z -= moveSpeed * Time.deltaTime;
}else if(targetPos == targetPos)
{
onPlatform = false;
}
Also, on the Rigidbody Component, you have a Constraints section where you can block the position and rotation on an axis. Unfortunately it will block all the axis not only positive/negative. Gjallanhorn answer would work for whta you want (or if your object do not have a rigidbody).
This is something I used. It limits the camera from moving up and down with the player when jumping or standing on higher platforms. You can use something like this, just make the axis you want to keep static as a set float and the others assigned to the players position.
public void Update()
{
if(!isDead)
{
if(playerObject.transform.position.x < mainCamera.transform.position.x)
camera$$anonymous$$ove = true;
}
if(!staticRoom)
{
//true, follow player
if(camera$$anonymous$$ove)
mainCamera.transform.position = new Vector3(playerObject.transform.position.x - .1f,
4.5f, mainCamera.transform.position.z);
}
}
@Gjallanhorn $$anonymous$$ind of silly of me to miss something like that but thank you. Did the job nicely.