Alright with some help from “aiursrage2k”, they were able to modify my script a make it better and also took a better approach by adding a bullet spread that increases on collision with the weapon power up. Increasing the spread amount form 2 to 3 to 4… and so on is a great way to achieve a weapon upgrade effect.
Now there are a couple minor errors I get with the script and I have been trying to get it to work. If anyone can help me out and figure out what we can do to get it working that’d be really appreciated. I’m making this an open to the public script so the faster we get this working the faster you can use it in your games. And I know people are looking for a script like this, I have scoured the forums and web and could not find one, but found hundreds of people looking for a weapon upgrade script. So lets get this thing working, there are only a few minor syntax errors that I can’t seem to fix, here’s the script and once again thanks to “aiursrage2k” for the help, he added the spread and collision code.
// Instantiates a projectile every 0.1 seconds,
// if the Fire1 button is pressed or held down.
var projectile : Rigidbody;
var fireRate = 0.1;
var weaponSpread = 25;
var projectilesPerShot = 3;
var Velocity : float = 25;
private var nextFire = 0.0;
function OnTriggerEnter(col:Collider)
{
if(col.tag == "WeaponUpgrade")
{
weaponUpgrade();
Destroy(col.gameObject);
}
}
//If there's a collision activate the following
function OnCollisionEnter(collision : Collision) {
// Debug-draw all contact points and normals
var gameObject;
for (var contact : ContactPoint in collision.contacts) {
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
// Play a sound if the coliding objects had a big impact.
if (collision.relativeVelocity.magnitude > 2)
{
audio.Play();
}
}
function upgradeWeapon()
{
//so lets give it more spread and more shots per fire.
weaponSpread+=10;
bulletsPerShot+=2;
}
function Update () {
//If the player holds down or presses the left mouse button
if (Input.GetButton ("Fire1") Time.time > nextFire) {
Shoot();
}
}
//To make our scripting a little more Object-Oriented-Programming, we will create our custom functions as well
function Shoot() {
//Add fireRate and current time to nextFire
nextFire = Time.time + fireRate;
//NEW CODES
var angle = 0;
if(nomProjectilesPerShot>0)
{
angle = -(weaponSpread *.5f)
}
var spreadInc = weaponSpread / nomProjectilesPerShot;
for(var i=0; i<nomProjectilesPerShot; i++)
{
//Instantiate the projectile
clone = Instantiate (projectile, transform.position, transform.rotation);
//Name the clone "Shot" ::: this name will appear in the Hierarchy View when you Instantiate the object
clone.name = "Shot";
var q:Quaternion = Quaternion.AngleAxis(angle,Vector3.up);
//Add speed to the target
clone.rigidbody.velocity = transform.TransformDirection (q * Vector3.forward * Velocity);
angle+=spreadInc;
}
}
If you look at the documentation, you will see that there is no detailed collision information available with a trigger. You are trying to reference something that doesn’t exist, hence the error.
Alright well where do I put the collision information at? I’m new and still learning programming. If you could help me out I’d really appreciate it, I already wrote the script I just need a little guidance and help.
Just a tip - when you post non working code to the forum that’s called “asking for help” not “generously donating it to the community”. It also contains such basic syntax errors that I’m guessing a lot of people were too turned off by your laziness to help you.
But sure, lets take a quick look…
First error :
angle = -(weaponSpread *.5f)
Lose the f, this is Javascript, not C. And put a ; on the end of the line while we’re at it.
Alright thanks for the tips. And I wasnt just posting a broken script, I said that if people can help out and get it working it’s there’s to use. I’ll try what you said and see if I can get it working. Thanks for the help
I think people whas angry over the misleading titel
You should have named it partially working script that neads input
But a nice thing to do when you are giving away some scripts for people in nead
Ik Animation - … are you even trying? At least post the error if you expect anyone to reply!
Or better yet, double click the first error, go to the line that’s causing the problem and stare at it for a while and see if you can work out what’s wrong. Then if you’re still stumped, come back and post what you know and maybe someone will help you.