• 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
Question by RaiderA528 · Sep 26, 2011 at 06:59 PM · instantiateprojectiledamagefiringhealth

Instantiate a projectile on key press "F" key / OnMouseDown! Help!!!

Okay so again I'm stuck, I want a prefab of a sphere that emits w$$anonymous$$ch I have created to get fired/(instantiated) out from the player_character I have when either I click the "left mouse button" or $$anonymous$$t the "F" key. The Sphere is tagged as "PlayerProjectile". I'm assuming it'd be somet$$anonymous$$ng on the function Update like function Update() { if(Input.GetAxis("Fire1") >.1) { instantiate."player projectile"; } } I know that's the wrong way it'd be written I just need a little help. Thanks!!! Also if anyone could throw it I have an enemy cube with a health GUI display and a health variable and i need a way in that enemy's health script to detect if its been $$anonymous$$t by one of these PlayerProjectiles and lose health accordingly. The Script on the Enemy Cube is - var hp:float; var maxhp:float; var healthbarWidth:int; var myHealthbar:GameObject; var myhb:GameObject;

function Start(){ healthbarWidth=20; myhb=Instantiate(myHealthbar,transform.position,transform.rotation); }

function Update(){

 myhb.transform.position=Camera.main.WorldToViewportPoint (transform.position);
 myhb.transform.position.x-=.05;
 myhb.transform.position.y-=.05;
 myhb.transform.localScale=Vector3.zero;
 var healthpercent:float=hp/maxhp;
 if(healthpercent<0){healthpercent=0;}
 if(healthpercent>100){healthpercent=100;}
 healthbarWidth=healthpercent*20;
 myhb.guiTexture.pixelInset=Rect(20,20,healthbarWidth,10);

} Of course that part won't do me any good until i can actually fire the projectiles. Thanks for any help in advance. : )

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Piflik · Sep 26, 2011 at 07:49 PM

 if(Input.GetKeyDown(KeyCode.F) || Input.GetKeyDown(KeyCode.Mouse0))
     Instantiate(Prefab, transform.position, Quaternion.identity);

Format your code correctly next time, please. It is hard to read as it is now.

Comment
J3-Gaming

People who like this

1 Show 0 · 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

Answer by RaiderA528 · Oct 04, 2011 at 01:10 PM

Sorry it messed up when I posted it. it was indented and everyt$$anonymous$$ng in the text box when i typed it in! : s

Comment

People who like this

0 Show 1 · 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 BiG · Oct 04, 2011 at 02:51 PM 0
Share

Also select @Piflik's answer as the correct one, if he helped you! :) The question will be marked as "solved" in this way.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enemy Health Damage 2 Answers

I want an enemy to take damage from a projectile (A Certain Layer Named Bullet) 1 Answer

how to take health/hearth from player when colliding with an object? 2 Answers

Firing only a single projectile at a time, then adding a second. 2 Answers

Prefab shooting damage/health? 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