I got this script from website. It works but need some improvement. I need to change the power of shoot. In this script there is a range “_angle” but it’s not what i want. I want to see a curve movement of the ball when hit target. automatic calculate the distance and pick the power ball needs to hit target in curve movement.
public Transform _Goal; // target
[Range(20f, 70f)] public float _angle; // shooting angle
public float taget_range; // how far from me
void Update()
{
taget_range = Vector3.Distance(gameObject.transform.position, _Goal.position);
if (Input.GetKeyDown(KeyCode.Space) && taget_range <= 20f)
{
Launch();
}
}
// Launches the cube to the target transform (_bullseye)
private void Launch()
{
// source and target positions
Vector3 pos = transform.position;
Vector3 target = _Goal.position;
// distance between target and source
float dist = Vector3.Distance(pos, target);
// rotate the object to face the target
transform.LookAt(target);
// calculate initival velocity required to land the cube on target using the formula (9)
float Vi = Mathf.Sqrt(dist * -Physics.gravity.y / (Mathf.Sin(Mathf.Deg2Rad * _angle * 2)));
float Vy, Vz; // y,z components of the initial velocity
Vy = Vi * Mathf.Sin(Mathf.Deg2Rad * _angle);
Vz = Vi * Mathf.Cos(Mathf.Deg2Rad * _angle);
// create the velocity vector in local space
Vector3 localVelocity = new Vector3(0f, Vy, Vz);
// transform it to global vector
Vector3 globalVelocity = transform.TransformVector(localVelocity);
// launch the cube by setting its initial velocity
GetComponent<Rigidbody>().velocity = globalVelocity;
}