Require some help in code.

Sorry for anoying, but I can´t make my code work.

So, this is what I soposed to do in the code.

The shooting works and all, but I want the code to shoot, if the unit “Patrulla1” is selected, so if the unit is select and press the key, it shoot, and if not selected, it doesn´t, so I can´t do that, it seems with bolean it works but i cant make it work.

BTW some help how can I make it shoot with an ammo limit, reload, and shooting limit like shoot every 3 secs?

Ok a friend help me a bit, and redid the code and did it better, but still can´t make it work.

function Update ()

{
if (activeUnit==true)
{
if(Input.GetButtonDown("Fire2"))
{
var Bullet = Instantiate(Bullet, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);

Bullet.rigidbody.AddForce(transform.forward * 5000);

}//if

}//if

else if (activeUnit==false){
}

}//function

Love unity, my teacher, likes the proyect I do in them! thx for the great program! :slight_smile:

Are you getting any NULL reference errors in the console?

Its says error to compile.

Added private var activeUnit : boolean = false;

But now it gives me a error on the “Bullet” unkown identifier.

OK, after looking at your code, you were referencing Bullet as something you were instantiating. What you need to do is differentiate Bullet the object you are copying, and Bullet the object you are using in the code. (not to mention, you can’t name them the same.)

Do yourself a favor, create a bullet at the end of the gun, Make sure to give it a rigidbody ONLY, no collider, then besides its name at the top uncheck the box for it. (disable it)

What follows are pieces for the components you need to fire at something.

First, lets assume that you have a gun, and you have to tell the gun that it is active. This is done when you pick up the gun, you reference the script and you set the activeUnit to true. Quite simple. Now, you instantiate the bullet that you had placed at the barrel of the gun and enable it, then add force to it to get it to move. You almost had this, but here is the updated code that should fix your problem.

var Bullet : GameObject;
var activeUnit=false;

function Update (){
	if (activeUnit==true){
		if(Input.GetButtonDown("Fire2")){
			var bullet = Instantiate(Bullet, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
			bullet.enabled=true;
			bullet.rigidbody.AddForce(transform.forward * 5000);
		}
	}
}

Next, as the bullet travels through the air it may or may not hit something. What we do is to check the last position against the current position and see if we hit anything in between. (remember I said the bullet should NOT have a collider so it wont hit its self.)

When it finally hits something, the bullet should 1) destroy it’s self, 2) send a message to the other object saying it should take some damage. So here is the code for the bullet. (very simple, more could be added to make it do things when it is destroyed)

var damage : float=5.0;
private var lastPosition : Vector3;

function OnEnable () {
	lastPosition=transform.position;
}

function LateUpdate(){
	var hit : RaycastHit;
	var ray : Ray=Ray(lastPosition, transform.position - lastPosition);
	var dist = Vector3.Distance(transform.position, lastPosition);
	if(Physics.Raycast(ray, hit, dist)){
		hit.collider.gameObject.SendMessage("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
		Destroy(gameObject);
	}
	lastPosition=transform.position;
}

The last thing is the part where something takes damage. This is a basic health script that could be integrated into anything, or just stand alone. It has the receiving function “TakeDamage” which is required by the bullet, and once it takes some damage, it asks if it is dead yet. If so, it destroys the object. (again, this could be far more complex depending how over dramatic your people want to die in.) So here’s the code:

var hitPoints=100.0;

function TakeDamage(amount : float){
	hitPoints-=amount;
	if(hitPoints <= 0.0){
		Destroy(gameObject);
	}
}

I really apreciate your work, but I ´cant make it work :frowning:

Did as you said, but the balls just dont get any power strait, just falls of, and even if the unit is not selected, it fires, (using Comunity RTS Generals project).

In the second code, if(Physics.Raycast(ray, hit, dist); needed an extra “)” ? and othercollider, give me error, and used collider, and it didn´t give me error.

Althow it gave me an null error.

And in the third code, it gave me an error as an unknown identifier in hitpoints.

Any help apreciated, :slight_smile: thx anyway there a good learning some stuff in codes :slight_smile:

Corrected the scripts not to give errors

Ok thx ill try to fix it! thx alot! :slight_smile: