In this case, I instantiate the ball fine, but the last lines where I set the BallInPlay to true isn’t being regsitered and I can fire more balls every time I hit and release the mouse (I am getting better at this coding stuff though!) …
/* ----- variables for gameplay ----- */
// identifies the ball object
var projectile : Rigidbody;
// variables to define how hard the ball gets kicked and how long it exists
// will need to add chargepower variable later
var BallSpeed = 10;
// a boolean that will be set to true or false to indicate if a ball is currently in play
var BallInPlay : boolean;
// we start the function when this comes alive setting BallInPlay to false to allow us to fire a ball
function Start () {
BallInPlay == false;
}
function Update () {
if (Input.GetMouseButtonUp (0) BallInPlay == false) {
KickBall();
}
}
function KickBall() {
ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
//if (audio !audio.isPlaying) {
// audio.Play ();
//}
// determines where to aim ball
if (Physics.Raycast (ray, hit)) {
target = hit.point;
}
else {
target = (ray.origin + ray.direction * 20);
}
direction = target - transform.position;
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, Quaternion.FromToRotation (Vector3.fwd, direction));
BallInPlay == true;
instantiatedProjectile.velocity = direction.normalized * BallSpeed;
yield WaitForSeconds (5);
instantiatedProjectile.active = false;
BallInPlay == false;
}
Also, I was using this …
Destroy (instantiatedProjectile,5);
instead of this line …
instantiatedProjectile.active = false;
But “destroy” doesn’t seem to remove the game object from the game, it simply freezes the ball and leaves it on the field as a collideable object (not desired). Will “instantiatedProjectile.active = false;” use up any extra CPU power or is the object gone forever?
Removed the “instantiatedProjectile.active = false;” and replaced with above … works fine thanks. Working on replacing the =='s now.
yes! Fixed it. Working perfectly now.
Now can I add a simple bit of code that gets the “instantiatedProjectile.velocity” if it’s less than say 0.1 and THAT destroys the ball instead of a number of seconds?
That is the idea but it isn’t working when placed inside the KickBall function. Should it instead go into the function Update to watch for the ball slowing down?