- Home /
How to generate a random trajectory when the ball is kicked ?
I'm trying to do a simple scene where a ball is kicked at the press of space bar.
Now I have this code that use AddForce()
to "kick" the ball with a random direction, but I'm not sure that is the correct solution because I set these parameters by trying and trying again setting them gradually.
There is the code:
private void FixedUpdate()
{
float randomY = Random.Range(0.1f, 1.3f);
float randomX = Random.Range(-0.2f, 0.2f);
Vector3 direction = new Vector3(randomX,randomY,1);
if (Input.GetButtonDown("Jump"))
_ballRb.AddForce(direction * kickForce,ForceMode.Impulse);
}
Its possible to create a sort of cone in which a trajectory is randomly generate?
Answer by AZOM2004 · Jun 20, 2020 at 03:13 PM
If you don’t want it so you have to try and adjust, it it because the “triangle of random” will be arranged in a different way (the ball further, the net wider...) because if that’s the case, I think you could use trigonometry to get the angles of the limits of the net. I am not very good at Unity and C#, but I can help with ideas...