Just some help with shooting and rigidbody :P

Hi! I am trying to make some sort of magical staff that can shoot a ball of light (simular to one in skyrim if you have played that).
So I found this script wich is pretty much what I want but is missing some details… I want the prefab to spawn and follow the player/camera when “fire1” is pressed and I want the force to be added when “fire1” is released! I also would like it so if the prefab collides with anything it stops and wont get knocked arond and bounce and that stuff…

Here is the script:

var prefabBullet:Transform;
var shootForce:float;
function Update(){
	if(Input.GetButtonDown("Fire1")){
		var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity); 
		instanceBullet.rigibody.AddForce(transform.forward * shootForce);
	}
}

Plz have in mind that I’m new to scripting so if there is anything thats a bit advance plz explain it clearly!

Tnx for your help!

//Elis

heh, a little searching on the forum, not to mention I just wrote it about an hour or so ago…

http://forum.unity3d.com/threads/163808-need-help-with-my-gun-script-first-try-(?p=1120606&viewfull=1#post1120606

This is not exactly what I was looking for and I don’t really understand all that code… :stuck_out_tongue:

And I have been searching a bit on the forum but mabye not enough… :stuck_out_tongue:

of my code, lines 35 to 38 do pretty much what you are trying to do here. The rest of the code handles sounds, reloading and ammunition.

I would bet if you slowed down, and read the code I put function for function, you would see the light of it. :wink:

Well, that code looks like it’s doing the same thing that my code does! (If I’m not wrong) Spawning in a bullet and then adding force to it so it goes forward!
What I want is so when you hold down “fire1” it “charges up” and when you release it the force gets added! Basicly Input.GetButtonDown(“Fire1”) = Spawning the bullet(Follows the camera as a child object would), Inout.GetButtonUp(“Fire1”) = Adding force to the bullet sending it the way that the player is currently looking.

Also, tryed this code on another computer were I was using the older verision of unity abd that worked fine! I just tryed it on my regular pc wich got the latest verision and I got this error:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)

If I could get help with that to?

Sry if I dont really understand or I’m not clear enough, I’m from Sweden so my english is not the best… :stuck_out_tongue:

when you press down, collect the time at that point, when you let up, use the time at that point - the start time, and you have a number. hold it down for 3 seconds, u get 3. :wink: Force/velocity are then simply multiplied by the result. I would clamp the value, like 1 to 3 or how long you want the player to hold it though, that way, if you click, it still goes someplace.

A null reference error is something that hasnt been set yet. I dont see a line number though. You can test for failures by using Debug.Log(“???”); (or something similar)

I would put this stuff all over my code and number them. Wherever the numbers end. that is my error. :wink:

Ok, could you plz explain it a bit more in detail? I haven’t script for so long so I’m pretty much a noob… :stuck_out_tongue:
Also could you fix together that code for me cuz I still dont really understand it… :stuck_out_tongue:

The error shows up when I press “Fire1”, the objects spawns but don’t move!

Tnx! :slight_smile:

???

shootForce ← this seems to be 0, set it to something like 1,2,5,10… etc

OK, its a timed event.

First, this is a simple Lobbed weapon script. you can see where I save the start time, and then use it when releasing the fire1 button to calculate amount.

Next, I go and create the bullet that I want to work with, and set it up similar to what you have.

Next, I add a little extra rotation to it. (it didnt lob as far as I wanted it to) This gives a better arc.

Last, I set the velocity of my bullet modified by the amount of time that I held the button down.

Demo:
http://www.lod3dx.net/Unity/Firebomb.html
(WASD == move, 1, 2 == weapon selection, mouse == look, scroll in and out as well as fire.)

Code:

#pragma strict

var bullet:GameObject;
var start:float;
private var canFire:boolean = true;

function Start () {
	bullet.SetActive(false);
	if(bullet.collider) Destroy(bullet.collider);
	if(!bullet.rigidbody) bullet.AddComponent(Rigidbody);
	print(gameObject);
}

function doUpdate(){Update();}

function Update () {
	//if(Input.GetKeyDown(KeyCode.Alpha1)) start = Time.time;
	//if(Input.GetKeyUp(KeyCode.Alpha1)) Fire(Time.time - start);
	
	if(Input.GetButtonDown("Fire1")) start = Time.time;
	if(Input.GetButtonUp("Fire1")) Fire(Time.time - start);
}

function Fire(amount:float){
	var velocity:float = Mathf.Clamp(amount * 40, 40, 400);
	var blt:GameObject = Instantiate(bullet, bullet.transform.position, Camera.main.transform.rotation);
	blt.SetActive(true);
	blt.transform.Rotate(-10,0,0, Space.Self);
	blt.rigidbody.velocity = blt.transform.forward * velocity;
}

The demo was made with the old Detonator package.

I also made a simple bullet, explosion instantiator in the bullet physics character controller, as well as a weapons controller. Simple project and took maybe 3 hours in tinkering and playing. Essentially, both weapons are the same scripts. :wink:

Test the demo and its very nice but still not exactly what I ment… :s

I found a video on youtube that showes of the staff from skyrim I was talking about!

Also I want the “bullet” or the ball of light to still be in the game when collide just that it stops(Not disepear or explode)! I also whant it so you have to hold the button down in maby 1 sec or it don’t shoot, not shoot stronger or weaker… :stuck_out_tongue:

shootForce is set to 1000 (the smallest amount to get it move forward in the tutorial)

Figured out some code by my selfe but still got that error… :stuck_out_tongue:

var prefabBullet:Transform;
var shootForce:float;
var charge:float;
var charging = false;
var staffLight:GameObject;

function Update()
{
	if(Input.GetButtonDown("Fire1"))
	{
		charging = true;
	}
	if(Input.GetButtonUp("Fire1"))
	{
		chargingOff();
	}
	
	if(charging){
		charge += Time.deltaTime;
		
		if(charge >= 1.0){
			if(Input.GetButtonUp("Fire1")){
				var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity); 
				instanceBullet.rigibody.AddForce(transform.forward * shootForce);
			}
		}
		staffLight.light.intensity = 5.0;
	}
	
	if(!charging){
		staffLight.light.intensity = 1.0;
	}
}

function chargingOff(){
	yield WaitForSeconds (0.25);
	charge = 0.0;
	charging = false;
}

Error:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[ ] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[ ] args, System.Type scriptBaseType)
LightBallShoot.Update () (at Assets/Scripts/Player Actions/LightBallShoot.js:24)

I have put up the force as suggested… :stuck_out_tongue:

looks like you have forgotten to assigne the bullet transform or the staffLight game object. however your ligth will not shoot because the “if(charging)” will be false since you set “charging = false” with chargingOff()

i advise you to search for some java skript guides or cooding guides in general.

I think I may have fixed it! :smile:

There was something wrong in the code I found (Just test it on my other pc), fixed it! Will try it on my main pc tomorrow and also the light will shoot since it already got the force added before it turns of… :stuck_out_tongue: At least it worked in unity 3…

if you fixed it pls post the solution so if somone find this thread over google and has the same probleme he can fix it easly.

it happens to me soo many times that i search for a problme, find the exactly same problem on a forum and he fixed it without posting the solution.

Here is the code:

var prefabBullet:Transform;
var shootForce:float;
var charge:float;
var charging = false;
var maxCharge : int = 1;
var staffLight:GameObject;

function Update(){
    if(Input.GetButtonDown("Fire1")){
        charging = true;
    }
    if(Input.GetButtonUp("Fire1")){
        chargingOff();
    }

    if(charging){
        charge += Time.deltaTime;
        
        if(charge >= maxCharge){
            if(Input.GetButtonUp("Fire1")){
                var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
				instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
            }
        }
        staffLight.light.intensity = 5.0;
    }
    
    if(!charging){
        staffLight.light.intensity = 1.0;
    }
}

function chargingOff(){
    yield WaitForSeconds (0.1);
    charge = 0.0;
    charging = false;
}

I also added this script to the bullet to make it stop on collision and also destroy it selfe after a period of time!

var hit = false;
var maxLifeTime : int;
private var lifeTime : float;

function Update(){
	if(hit){
		rigidbody.isKinematic = true;
	}
	
	lifeTime += Time.deltaTime;
	
	if(lifeTime >= maxLifeTime){
		Destroy(gameObject);
	}
}

function OnCollisionEnter(collision : Collision){
	hit = true;
}

Here’s how it turned out! :smile: