Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mister.keith · Jul 21, 2012 at 11:47 PM · rotationtransformvector3

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);
     }
 
 }
     }

alt text

screen shot 2012-07-24 at 2.24.10 pm.png (100.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ScroodgeM · Jul 22, 2012 at 07:27 AM

  • first
    transform.TransformDirection(Vector3.forward * 10)
    is same as
    transform.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;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mister.keith · Jul 24, 2012 at 06:26 PM 0
Share

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!

avatar image ScroodgeM · Jul 24, 2012 at 07:04 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I align my player transform to waypoints and without restricting Z Axis rotation? 2 Answers

Keep camera at certain distance from character when rotating 3 Answers

Instantiate GameObject towards player 0 Answers

Transform a Vector by a Quaternion 1 Answer

Rotations and waypoints 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges