Hello everyone,
Just having an issue with Instantiate and the rigidbody moving forward, I have the object set with the correct axis and when I press the fire button the bullets just fall down instead of going forward. Any ideas why?
Here is my code snippet:
void fireMissile()
{
//clone the missiles
Instantiate(bullets,transform.position,transform.rotation);
bullets.rigidbody.velocity = transform.TransformDirection(Vector3.forward * 10);
}
Thanks.
Can I assume that bullets is a prefab?
Perhaps on the rigidbody within the prefab the missile is set to use gravity which will cause it to drop?
Thanks for the reply and I am sorry I am late I did something different to it and got it to work, then I got so consumed I forgot to come delete the post. Sorry about that but here is the code that I used.
void fireMissile()
{
GameObject clone;
//clone the missile
clone =(GameObject) Instantiate(bullets,transform.position,transform.rotation);
clone.constantForce.relativeForce = new Vector3(0,0,speed);
Destroy(clone.gameObject,5);
}
I have a new question so instead of taking up space I am going to post here:
How would you setup multiple triggers, I have a spacecraft going through a trigger to fire a gun but nothing happens, any suggestions?
I am assuming you mean having a box collider that is set to is Trigger?
If so then you could do something similar to:
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")//or spaceship or enemy or anything that the game object is tagged as to only allow it to fire a missile
{
other.gameObject.SendMessage("fireMissile");
}
}
In a script placed on the box collider, I do believe something like that would work.
The send message is not the most resource friendly way to perform this task but it will get the job done, so here’s the other way:
void Start()
{
objPlayer = (GameObject) GameObject.FindWithTag ("Player");
FireGunScript = (FireGunScript) objPlayer.GetComponent( typeof(FireGunScript) );
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")//or spaceship or enemy or anything that the game object is tagged as to only allow it to fire a missile
{
FireGunScript.fireMissile();
}
}
Yeah I tried that the problem was that I was calling a trigger from both scripts and it couldn’t tell which trigger I was calling from but I had another method, I used the good ole 3D distance formula to check the distance between the objects and then fire the weapon. I am so in a rush to put out this project I am forgetting programming 101.
I have everything instantiated but I have another issue, how do I instantiate one projectile at a time from an AI cannon. Sorry for not knowing this been doing some searching trying to find the answer.
Here is the code for the AI instantiation.
//bullet to fired
public void shootBullets()
{
//clone the bullet
GameObject cbullet;
cbullet = (GameObject)Instantiate(bullet,barrel.position,barrel.rotation);
cbullet.constantForce.relativeForce = new Vector3(0,0,speed);
Destroy(cbullet.gameObject,2);
}
So could anyone help? Please??
What exactly goes wrong when you try to use this code? If the bullets are appearing in the hierarchy view, but aren’t visible onscreen, it’s possible they are colliding with the gun itself. You can use Physics.IgnoreCollision to stop this from happening.
The problem is that it is not shooting out a bullet one at a time it is firing multiple bullets, which I understand about instantiate(at least I thought I did) but how do I get it shoot one at a time?
SO does anyone know how to make a instantiate object fire one object at a time, not from my weapon but from an AI weapon.
Shout out to MartinB for helping me solve this issue, dude you are boss.