Hello. Maybe is a difficult question, but im really noob, so please if someone can help me, would be great
I have a gun, and a fire button. This is ok in my game. I also have a 3d model of a bullet. What i want is that when the user press the fire button, the gun fires the bullet. Maybe is really noob, or maybe is really difficult, but i would really appreciate any help.
Thanks in advance
You are going to want to instantiate a bullet prefab with code. Make sure the prefab has a script that gives it a force.
I would put this code on your gun:
var bulletObject : GameObject;
var gunObject : GameObject;
function Update() {
if (Input.GetKey(KeyCode.Space)) {
var instantiatedBullet : Rigidbody = Instantiate (bulletObject, GameObject.gunObject.transform.position, GameObject.gunObject.transform.rotation);
}
}
Add this script to your gun and then link the 2 variables in the hierarchy to the gameobjects of your choice.
Oh, i forgot something. Im developing for iPhone. Does the code changes or is the same?
I don’t have much expeirience… well actually I have noe with Iphone. But Unityscript works with Iphone so I presume it will be just fine.
okay, ijust try it and get
‘gunObject’ is not a member of ‘UnityEngine.GameObject’.
Whoops, my bad. Try this:
var bulletObject : GameObject;
var gunObject : GameObject;
function Update() {
if (Input.GetKey(KeyCode.Space)) {
var instantiatedBullet : Rigidbody = Instantiate (bulletObject, gunObject.transform.position, gunObject.transform.rotation);
}
}
Great, now it works, but i still have a problem. Here is my code now
bullet.gameObject.rigidbody.AddForce (0,0,Fuerza);
var instantiatedBullet : Rigidbody = Instantiate (bullet, gun.transform.position, gun.transform.rotation);
When i click the fire button, another bullet is created. It is named “bullet(clone)”
The problem is that just the original is getting a force. So, the other bullets just fall down and doesnt get launched.
Im getting this error on unity
InvalidCastException: Cannot cast from source type to destination type.
Fire .OnGUI () (at Assets/Level 1/Fire .js:12)
Thanks for your help
I see. Well the problem there is that you are making a bullet move, but you are not making the instantiated object move. Instead of
bullet.gameObject.rigidbody.AddForce (0,0,Fuerza);
var instantiatedBullet : Rigidbody = Instantiate (bullet, gun.transform.position, gun.transform.rotation);
try making the instantiatedBullet move like so:
var instantiatedBullet : Rigidbody = Instantiate (bullet, gun.transform.position, gun.transform.rotation);
instantiatedBullet.rigidbody.AddForce (0,0,Fuerza);
I believe this should work.
Okay i found another way and this one works
Just i create a script named Force on my bullet and in the update i put
rigidbody.AddForce (0,0,1000);
But, the problem now is the bullet is just pointing to one direction. Even if the gun is pointing to the other one
Thats odd. Does the bullet have a rigidbody? If so make sure freeze rotation on the bullet is not set.
First. I would use killer1390’s way of adding force because of the second reason.
Second. Adding force every frame will cause the object to speed up the longer it is in existence which is reverse real life. Killer’s way adds it at the beginning and it slows down naturally.
Third. AddForce is in world space and you want it to go forward in local space so do this.
var forward = transform.TransformDirection(Vector3.forward);
instantiatedBullet.AddForce (forward * 1000);
Fourth and finally. This question is answered in the FPS tutorial and many other simple problems are too so please feel free to post here (We’re glad to help), but please look through them. That’s how I found was the easiest way to learn.
Hi Guys
I have gone through the post its very useful, how ever Iam still unable to shoot my bullet out of the gun. I am using “C Sharp” as my programming language following the code that i am using for the same :-
public class Bullet_Script : MonoBehaviour{
public GameObject bulletObject;
// Use this for initialization
void Start(){
}
// Update is called once per frame
void Update(){
Shoot ();
}
void Shoot(){
Rigidbody instantiatedBullet;
if (Input.GetKey(KeyCode.Space)){
instantiatedBullet = (Rigidbody) Instantiate (bulletObject, this.transform.position, this.transform.rotation);
instantiatedBullet.rigidbody.AddForce (0,0,10);
}
}
}
can any one tell me were I am going wrong ? pls
NOTE: I am new to Unity Programming.
@raviraj What is your specific problem. There are lots of reasons that your gun won’t shoot a bullet. My guess would be that your bullets are flying in the wrong direction based off your code.
You want to add force in the local forward direction. Rigidbody.AddForce(transform.forward * 10);
Firstly My code is giving me the following error :
“Cannot cast from source type to destination type.”
Secondly I rewrote the code the the above error did not come however the bullets were firing in the wrong direction.
Thirdly the bullets were not getting destroyed even when I used the following code :
“Destroy (instantiatedBullet, 5);”
See my post right before yours on why your bullets are firing in the wrong direction.
- If you wrote your code exactly how it appears in this thread, then you have to use
Destroy(instantiatedBullet.gameObject, 5);
Hey thanks for helping ,I will try it out n let u know 
also my code is exactly as it in the thread.
thanks Again il let u know