- Home /
How to freeze z-axis movement?
Hello everyone! I am making a mobile game, which is controlled via mobile accelerometer sensor. I made a ball which should hit a cube. I want to firstly make the movement in only z-axis, after a known distance is reached i want to freeze the movement in z-axis and allow only movement in x-axis. I wrote the following script and checked the x constraint in the object rigidbody, but movement isn't right.
float z = Input.acceleration.y;
float x = Input.acceleration.x;
Vector3 CubePosition = Cube.position;
d = Vector3.Distance(CubePosition, transform.position);
transform.Translate (0, 0, z * Time.deltaTime* 5);
if (d <= 3.2) {
transform.Translate (x * Time.deltaTime * 5, 0, 0);
}
Answer by 123_umer · Nov 01, 2018 at 09:30 AM
i want to freeze the movement in z-axis and allow only movement in x-axis. I wrote the following script and checked the x constraint in the object rigidbody, but movement isn't right.
if you want to freeze the movement in Z-axis then you should check the constraint z-axis not x-axis. and also take a look at this unity doc for rigidbodyconstraint.freezePostion. https://docs.unity3d.com/ScriptReference/RigidbodyConstraints.FreezePosition.html
I checked x-axis, because i want the ball to move in the beginning only in z-axis, and after known distance i want to freeze the z-axis movement and allow only movement in x-axis.
Answer by TheDavvyd · Nov 01, 2018 at 09:48 AM
float z = Input.acceleration.y;
float x = Input.acceleration.x;
Vector3 CubePosition = Cube.position;
d = Vector3.Distance(CubePosition, transform.position);
if (d <= 3.2) //you can make a bool var, so that condition is more flexible
transform.Translate (0, 0, z * Time.deltaTime* 5);
else
transform.Translate (x * Time.deltaTime * 5, 0, 0);
Or this may be helpful https://docs.unity3d.com/ScriptReference/RigidbodyConstraints.html
Your answer
Follow this Question
Related Questions
Accelerometer 2 Answers
Bag Toss Unity Accelerometer 1 Answer
accelometer problem 0 Answers
Accelerometer axis differences on Android and iOS? 1 Answer
how to hang a 3d model hanging seamlessly from a camera 1 Answer