# kick a ball

Hello I am making a soccer game I want that the player can shoot the ball by using the left mouse button the problem is i have no script and I ask the community for help not for the whole script but for hints (the whole script is good to) I have explain what i want

1. I want how longer you hold the mouse button pushed how harder you shoot(sorry for the bad sentence)
2. i want a shoot angle between 0 and 20 degrees random

sorry that i have no script

and tank you so much for helping me

This script makes the ball move forward (along z-axis) and bounce after landing. It limits force, applied to ball by tMax. Force is applied only if mouse cursor points the ball.

Attach RigidBody component to the ball, Use Gravity must be checked, adjust Drag value.

using UnityEngine;
using System.Collections;

public class KickScript : MonoBehaviour {

public float bounceFactor = 0.9f; // Determines how the ball will be bouncing after landing. The value is [0..1]
public float forceFactor = 10f;
public float tMax = 5f; // Pressing time upper limit

private float kickStart; // Keeps time, when you press button
private float kickForce; // Keeps time interval between button press and release
private Vector3 prevVelocity; // Keeps rigidbody velocity, calculated in FixedUpdate()

void Update()
{
if(Input.GetMouseButtonDown(0))
{
kickStart = Time.time;
}

if(Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if(hit.collider.name == "Ball") // Rename ball object to "Ball" in Inspector, or change name here
kickForce = Time.time - kickStart;
}
}
}

void FixedUpdate () {

if(kickForce != 0)
{
float angle = Random.Range(0,20) * Mathf.Deg2Rad;
forceFactor * Mathf.Clamp(kickForce, 0.0f, tMax) * Mathf.Sin(angle),
forceFactor * Mathf.Clamp(kickForce, 0.0f, tMax) * Mathf.Cos(angle)),
ForceMode.VelocityChange);
kickForce = 0;
}
prevVelocity = rigidbody.velocity;

}

void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Ground") // Do not forget assign tag to the field
{
rigidbody.velocity = new Vector3(prevVelocity.x,
-prevVelocity.y * Mathf.Clamp01(bounceFactor),
prevVelocity.z);
}
}

}

basically you are going to only ever have 1 ball in the soccer game so make a variable.
this script goes on to the player.

//drag the ball onto the spot create by this line
public Gameobject ball;
public float power = 0;

//if mouse button is down
if(Input.GetMouseButtonDown(0))
{
power =+ Time.Deltatime
{

//if mouse is not down
if(!Input.GetMouseButtonDown(0))
{
if(power > 0)
{
kickball();
{
power = 0;
{

//when the player is in range to shoot ball
void Kickball()
{
var Vector3 angleX = this.trasform.forward;
ball.getcomponent<rigidbody>().velocity = new Vector3(angleX.x * power,angleX.y* power + Random.Range(0,20),angleX.z* power);
}