- Home /
Player doesn't shoot ball object forward like I tell him
Hello guys, here is the problem,
I am making a soccer game where a player can shoot a ball in front of him, just like in real soccer.
The problem is, when I shoot, the ball does not go straight forward like planned, but goes diagonnaly : at 5 (clockwise) when looking right, and at 7 when looking left. Here's a screenshot for better comprehension.
*green arrows : wanted direction *red arrows : actual and unwanted direction
Here is the code of the shoot function :
void PassBall(float passCount)
{
ball.Pass(passCount * player.transform.forward);
//passCount is an int variable that increments each a pass occures. It is used the define the power of the shot
}
The function above is used in the playerController Script, when shot button pressed. It calls the following function, which is defined in the ball script :
//Passes the ball
public void Pass(Vector3 passForce)
{
if (!ballActive)
{
transform.parent = null;
gameObject.AddComponent<Rigidbody>();
Ball.GetComponent<Rigidbody>().AddForce(new Vector3(passForce.x * 5,passForce.y *5 + angle,passForce.z * 5), ForceMode.Impulse );
}
}
I can't figure out what Im doing wrong, can u please bring some help ? Thanks in advance for ur attention.
In the Pass function where you are applying force, there is some angle added to y, is that making the difference?
Hi, the angle is something returned by another function that allows the player to aim his shot, this is why it is added to the y axis, it works well, and I tried removing it, same problem.
Check with the value of player.transform.forward. Try to log it and check whether that is really the forward direction of the player.
Answer by alphanike97 · Dec 18, 2017 at 05:41 PM
Hi! Why do you sums pass force + angle? AddForce(new Vector3(passForce.x * 5,passForce.y *5 + angle,passForce.z * 5), ForceMode.Impulse );
Check if the Line Code was Right
Angle is returned by another function to let the player aim his shot. It works fine, but in order to check if it was the problem, I tried removing it, but the problem was still there.
Answer by jawadahmeddogar · Jan 19, 2018 at 04:30 PM
try to uncheck gravity on ball's rigidbody component and then see what happens.
Your answer
Follow this Question
Related Questions
transform.forward makes my GameObject Disappear 0 Answers
2D Shooter - shooting forward at the current rotation 2 Answers
raycast is pointing in two directions? 1 Answer
What is the error? 2 Answers