- Home /
 
Multiple Fire on different launcher GameObjects
Currently working on a tank simulator and I am having issues with the scripting of my launchers. I have two GameObjects that act as instantiation points (where the projectiles will be fired) one is for a machine gun the other for the main cannon. The code I have works if I split it up into two different scripts and attach them directly to the launchers G.O's themselves, but I feel thats a little ridiculous and not efficient. Currently I am stuck with this code. Any ideas how to remedy the situation?
Thanks!
-Keith-
 public Rigidbody machineGunPrefab;
 public Rigidbody mainCannonPrefab;
 public GameObject machineGunLauncher;
 public GameObject cannonLauncher;
 
 void Update () 
 {
 
 if(Input.GetMouseButtonDown(0)) //if you left click on the mouse...
 {       
 Debug.Log("Machine Gun");
 Rigidbody clone;
         clone = Instantiate(machineGunPrefab, machineGunLauncher, machineGunLauncher) as Rigidbody;
         clone.velocity = transform.TransformDirection(Vector3.forward * 10);
 } 
 if(Input.GetMouseButtonDown(1)) //if you right click on the mouse...
 {      
 Debug.Log ("Main Cannon");
         Rigidbody clone;
         clone = Instantiate(mainCannonPrefab, cannonLauncher, cannonLauncher) as Rigidbody;
         clone.velocity = transform.TransformDirection(Vector3.forward * 500);
     }
 
 }
     }
 
               
Answer by ScroodgeM · Jul 22, 2012 at 07:27 AM
-  first 
transform.TransformDirection(Vector3.forward * 10)
is same astransform.forward * 10
second you need not parent transform's forward direction, but a cannon's, so use machineGunLauncher or cannonLauncher instead of 'transform' in velocity calculation line
third as i remember Instantiate method needs obj, position and rotation as parameters, so you should use
clone = Instantiate(mainCannonPrefab, cannonLauncher.position, cannonLauncher.rotation) as Rigidbody;
 
finally this should looks like
Rigidbody clone; clone = Instantiate(machineGunPrefab, machineGunLauncher.position, machineGunLauncher.rotation) as Rigidbody; clone.velocity = machineGunLauncher.transform.forward * 10f;
I gave your answer a shot and it still is giving me the same errors. I'm looking to it a little further, but in the meantime, I uploaded a picture of the code and the errors $$anonymous$$onoDevelop is giving me. Thanks!
my little mistake. you can fix it yourself 8) i use launchers as Transforms but it is GameObjects (usually i declare Transforms for other objects, more often needs Transform then GameObject)
so fix variant 1:
public Transform machineGunLauncher; public Transform cannonLauncher;
ins$$anonymous$$d of GameObjects declaration
fix variant 2:
replace
machineGunLauncher.position
with
machineGunLauncher.transform.position
and so on
sorry for this little mistake
Your answer