Hello all, im making soccer game for smartphones. My question is how to limit player to kick the ball within goal limit? Please look at the following attachment Sorry for my poor English
I want somthing similar to this, Quaternion targetRotation = Quaternion.LookRotation (goalPosition.position - transform.position); transform.rotation = targetRotation;
I'm also not clear on what your asking but based on the code snippet I don't think your asking how to detect when the ball enters the net (as mentioned colliders is probably easiest for that). Are you asking how to have your player always shoot the ball towards the goal (in that direction)? If so I think more details are still probably needed. 1. Do you have a character and are you controlling the shot direction by a rotation on the character (meaning shot will always be in the direction the player is rotated towards)? 2. If not are you just looking to find the correct Vector3 to apply the force for the ball to go in that direction? Not quite sure the specific question you're after.
@Shukerullah Read this and post your question to this section if it still makes no sense. EDIT: And this
Please look at following screenshot, I want Player to kick the ball toward goal post. This following code works properly, but problem is player kick the ball always in same direction. Player can kick anywhere he want but within goal post. if(Shoot) { Quaternion targetRotation = Quaternion.LookRotation (goalPosition.position - transform.position); transform.rotation = targetRotation; ball.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(transform.forward.x*30.0f, 5.0f, transform.forward.z*30.0f ); }
not random actually, if player rotation is toward goal post then add forward velocity in a ball, else if Player rotation exceed goal then limit player rotation
If the player can actually choose the point where the ball is kicked, that's quite different. You could check if the selected point (when projected into world space) lies somewhere inside the edges of the goal. Or you could add a box collider trigger to the goal and do a raycast in the player's direction from the ball's position and check for a hit against the box collider.
I solved it by keeping Cube inside the goal. Position of cube is controlled by player rotation and cube is limited inside the edges of the goal. then i used following code and it worked if(Shoot) { Quaternion targetRotation = Quaternion.LookRotation (cube.transform.position - transform.position); transform.rotation = targetRotation; ball.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(transform.forward.x*30.0f, 5.0f, transform.forward.z*30.0f ); } Thank you all