Trouble in freezing movement in z axis

Hi everyone! I am making a mobile serious game for forearm rehabilitation; one of the exercises depend on arm extension then moving the arm in the x axis, a ball represents the arm movement and a target “brick” should be hit. I used the script below to firstly achieve only z axis movement, then when reaching a specific distance allowing only movement in x axis. To achieve the exercise every time the ball hits the brick, the ball is retrieved to start position, but what happens is that when the ball returns to the start position, movement is allowed in x axis, however i disabled it in the code. Any help?

void FixedUpdate () {
    		float x = Input.acceleration.x;
    		float z = Input.acceleration.y;
    		Vector3 CubePosition = Cube.position;
    		Vector3 PlayerPosition = transform.position;
    		dz = Mathf.Floor(CubePosition.z-PlayerPosition.z);
    		dx = Mathf.Floor (CubePosition.x-PlayerPosition.x);
    		if (dz > 0) {
    			rb.AddForce (0, 0, z * mainSlider.value);
    		}
    			else {
    				rb.AddForce (x * mainSlider.value, 0, 0);
    			 
    		}

The code is divided between two scripts, one attached to the ball which is above, the other attached to the brick :-

private void OnCollisionEnter (Collision collision)
	{
		
		if (gameObject.tag == "Brick") {
Ball.GetComponent<Renderer> ().enabled = false;
		Ball.transform.position = new Vector3 (.43f, 0.42f, -.2f);
		Ball.transform.Translate (0, 0, Input.acceleration.y);
		Ball.GetComponent<Renderer> ().enabled = true;
}
}

Just a guess, but it may be because the ball still has velocity in those directions when it collides. All you are doing is just resetting its position, but it will maintain some velocity.


Try resetting the velocity in the OnCollision function:


 private void OnCollisionEnter (Collision collision)
     {
         
         if (gameObject.tag == "Brick") {
 Ball.GetComponent<Renderer> ().enabled = false;
         Ball.transform.position = new Vector3 (.43f, 0.42f, -.2f);
         
         // Reset velocity \\
         Ball.rigidbody.velocity = Vector3.zero;  // this is just pseudo to give you an idea

         Ball.transform.Translate (0, 0, Input.acceleration.y);
         Ball.GetComponent<Renderer> ().enabled = true;
 }