I have already done a projectile using mouse buttons and all. Now everytime i kick a ball there should be a random wind amount so that i kick the ball and the force changes according to the wind. For eg take the pocket tanks game evertime it generates a random wind value. That random wind i can generate through random function but i am not getting how to change the force direction and all.
Here is the script i have used
Rigidbody initialBall = null;
public Rigidbody ball;
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
float xAngle, yAngle;
void Start ()
{
SpawnBall();
}
public void SpawnBall()
{
initialBall = (Rigidbody)Instantiate(ball, transform.position + new Vector3(0, 0.3f, 3f), Quaternion.identity);
}
void Update ()
{
if(initialBall)
{
if(Input.GetMouseButtonDown(0))
{
firstPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
}
if(Input.GetMouseButton(0))
{
secondPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
currentSwipe.Normalize();
if(currentSwipe.y > 0 )
{
yAngle += currentSwipe.y * 40f;
}
if(currentSwipe.x < 0 )
{
xAngle += currentSwipe.x * 20f;
}
if(currentSwipe.x > 0 )
{
xAngle += currentSwipe.x * 20f;
}
}
if(Input.GetMouseButtonUp(0))
{
if(yAngle> 300)
initialBall.AddForce(xAngle, yAngle, yAngle);
xAngle = 0f;
yAngle = 0f;
}
}
}