2D normalise z rotation


I’ve got an issue with a simple game mechanic in a 2D game and was hoping to get some help. The idea is that I’ve got a ball inside a box. The ball is bouncy and as such can bounce off the inside walls of the box. The user is also able to click the ball and this applies force to it. I always apply an upward force, but would like to apply either a left or right force on the x axis depending on where they hit the ball. This works when I first launch my game, however once the ball has bounced and rotated the direction is getting reversed.

I know I basically need to check the z rotation and act on it accordingly, but I just can’t get my head around what I should do here. Below is the sample code that handles this

  void FixedUpdate ()

			if (Input.GetButton ("Fire1")) {
					Vector3 clickedPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
					RaycastHit2D hit = Physics2D.Raycast (clickedPosition, -Vector2.up);

                    if (hit && this.kicking) {

					this.rigidbody2D.isKinematic = true; //reset
					this.rigidbody2D.isKinematic = false;

                    Vector3 hitPoint = this.transform.InverseTransformPoint (hit.point);
						float zRotation = this.transform.rotation.eulerAngles.z;

				Debug.Log("Z rotations is " + zRotation.ToString());

							if (hitPoint.x < 0) {
								leftForce = maxVel;
							} else {
								leftForce = -maxVel ;
			                if(zRotation > 180){
				           leftForce = -leftForce;

							rigidbody2D.AddForce (new Vector2 (leftForce, topForce));

On top of this, is there anyway that I could make my “Fire1” event also act like a bounce, in that it would effect the zRotation? I’m guessing I would actually have to use a rigidbody with a Vector3 rather than a RigidBody2D for this.


I’ve managed to figure this one out. For anyone else with this issue, rather than using the methods above and calling rigidBody2D.AddForce I now use

 rigidbody2D.AddForceAtPosition(new Vector2(maxVel,topForce), new Vector2(hitPoint.x,hitPoint.y));