- Home /
Clamping "X" Position Movement On Platform (Code Inside)
Hello Is it Possible to Clamp "X" axis movement Position on These Codes? Or Can we add something else? It is just a platform and Player moves on the Z axis.
Void Update()
{
MoveVector = Vector3.zero;
//Touch Horizontal difference will be change the _horizontal value;
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
Pos = transform.position;
MoveVector.x = (Pos.x + touch.deltaPosition.x) * moveSideDelta;
if (touch.deltaPosition.x > touchDistanceDeltaToFace || touch.deltaPosition.x < -touchDistanceDeltaToFace)
{
Vector3 face = controller.velocity;
face.y = 0;
transform.forward = (Vector3.Lerp(transform.forward, face, Turn_speed));
}
}
if ((touch.phase == TouchPhase.Ended))
{
transform.forward = Vector3.forward;
}
}
MoveVector.x = Mathf.Clamp(MoveVector.x, -3f, 3f);//I think This Clamps The Amount Of Moving Distance to Left and Right at a time.
MoveVector.z = PlayerSpeed;
controller.Move(MoveVector * Time.deltaTime);
}
Answer by unity_BUJQdslYm7EuFQ · Jul 17, 2021 at 08:43 AM
If you want to clamp moving at x position, replace
MoveVector.x = Mathf.Clamp(MoveVector.x, -3f, 3f);
on
MoveVector.x = transform.position.x;
Your player will not move at this axis. If you meaned something else, please write this below.
Answer by Ercova · Jul 17, 2021 at 03:59 PM
@unity_BUJQdslYm7EuFQ Thanks For Reply. But i want my character move along X axis but lets say between -1 to 1.
Your answer
![](https://koobas.hobune.stream/wayback/20220613040417im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
How to predict position of object before transform.translate will apply? 1 Answer
getting a GameObject array of all the connected players 1 Answer
Camera Follow Player using a path 0 Answers
Photon pun 2 how to get the position of a Photon.Realtime.Player 0 Answers