Hi guys, I’m a novice when it comes to writing scripts in any language. What I’m trying to do is fire a prefab I made (cannonball) out of a cannon every 2 seconds at a random velocity each time it fires. The cannon is supposed to fire once the scene loads. When the prohectile is fired it is going to hit a target and when it does, it is supposed to be teleported to the right side of the screen to be collected. Hope that wasn’t too confusing. What I need help with is the random velocity every 2 seconds and when the projectile hits the target and gets transported. Here is my code thus far. To test if things worked I made it so the ball with the left mouse button. I want this changed obviously to the constant, every 2 seconds when the scene loads.
#pragma strict
//Attach to empty object, in front of barrel
var projectile : Rigidbody; //The Bullet
var speed = 250; //Speed Of The Bullets
var fireRate = .5;//Time Delay Between Shots!
function Update()
{
InvokeRepeating("Fire",fireRate,0.3); //Shoot Delay
}
//function Fire()
{
if(Input.GetMouseButtonDown(0))
{
//audio.Play();
var gunShot : AudioClip;
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
audio.PlayOneShot(gunShot);
CancelInvoke();
}
}
@script RequireComponent(AudioSource)
Its probably something very little I have to do, knowing my luck. I just can’t see it. Once again, I’m a noob at this stuff. I understand the code, but to put what I want to do into code is another thing. I’m scripting in JavaScript, I’m just starting to learn C#. Thanks a bunch!