Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Trouble in freezing movement in z axis

$
0
0
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 ().enabled = false; Ball.transform.position = new Vector3 (.43f, 0.42f, -.2f); Ball.transform.Translate (0, 0, Input.acceleration.y); Ball.GetComponent ().enabled = true; } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>