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!
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…
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. 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.
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…
Also could you fix together that code for me cuz I still dont really understand it…
The error shows up when I press “Fire1”, the objects spawns but don’t move!
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.
#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.
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…
shootForce is set to 1000 (the smallest amount to get it move forward in the tutorial)
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.
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… At least it worked in unity 3…