• 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 ForeignGod · Mar 08, 2013 at 08:07 PM · clonesprojectilesthrowing

Cloning help?

Hello, I've been making a small game with Kunai's, Katana's and such and i know the basics of scripting. The Kunai is an throw-able weapon and i wanted it to be cloned, thrown and then destroyed with this script which seemed to work

 var projectile : Rigidbody;
 var speed = 20;
 var lifetime = 1;
  
 function Update () {
 
     if (Input.GetMouseButton(0)) {
 
     var clone : Rigidbody;
     clone = Instantiate(projectile, transform.position, transform.rotation);
     
 
     clone.velocity = transform.TransformDirection (Vector3.forward * speed);
     
 
     Destroy(clone.gameObject, lifetime);
     }
 }

but the script clones several copies after one click, not just one at a time as i wish.

alt text

Can anyone please help me with this?

Thanks

193a819c1ef189fca6e3c67dcb2f5af8.png (2.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

Answer by robertbu · Mar 08, 2013 at 08:52 PM

You want to use Input.GetMouseButtonDown(0) instead of Input.GetMouse(). Input.GetMouse button will return true for any frames in which the mouse is held down. Input.GetMouseButtonDown(0) only return true for the frame in which the mouse is pressed.

Comment
Add comment · Show 5 · 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 ForeignGod · Mar 08, 2013 at 10:03 PM 0
Share

It kinda worked, but i think it is now cloning every $$anonymous$$unai in the scene, If i press once 1 kunai appears, if i press again 2 more appears, and the 3rd time i press 3 more appears. Could it be Cloning not only the "original" $$anonymous$$unai but also the clones?

avatar image robertbu · Mar 08, 2013 at 10:35 PM 0
Share

There are a few things here that are non-standard. I believe it is the Destory call that is causing the problem, but I'm not sure. Try these changes to your script:

 var projectile : GameObject;
 var speed = 20.0;
 var lifetime = 1.0;
  
 function Update () {
  
     if (Input.Get$$anonymous$$ouseButton(0)) {
  
     var clone : GameObject;
     clone = Instantiate(projectile, transform.position, transform.rotation);
  
  
     clone.rigidbody.velocity = transform.TransformDirection (Vector3.forward * speed);
  
  
     Destroy(clone, lifetime);
     }
 }
avatar image ForeignGod · Mar 08, 2013 at 10:42 PM 0
Share

Wellalt text

There are now extremely many clones inside of themselves + they are freezing in mid air.

and btw thanks for trying to help me out.

1f8aa0a7c7067ac8bf72baba6690afa0.png (279.6 kB)
avatar image ForeignGod · Mar 08, 2013 at 10:50 PM 0
Share

I think a friend of $$anonymous$$e found out the problem, the clones have the script and therefore they clone each other. But that's yet another problem to solve.

Is there a way i can make the clones spawn without the script?

avatar image robertbu · Mar 09, 2013 at 01:49 AM 1
Share

Typically I attach a cloning script to an empty game object in the scene. If the clones need to be in random locations, I just generate random numbers. If the close must happen at positions of existing clones, then you can get the positions of all the existing clones (through a tag) and select the position of one to be a the clone point.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

changing from line renderer to projectile 0 Answers

How to fire projectile in direction character is facing? 3 Answers

collison on trigger for multiple clones 1 Answer

clones dying at the same time. why? 1 Answer

How to clone randomly 3 Answers


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