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 Triqy · Aug 12, 2013 at 07:19 AM · raycastmobileenemydamageloops

What is the Best way to use Raycast for enemy Fire?

I want to use ray cast to damage my player with a for loop. How would I do this? I am not to familiar with for loops. Please show me how to correctly use a for loop.. thank you!

Comment
Add comment · Show 2
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 Triqy · Aug 12, 2013 at 08:10 AM 0
Share

Ahhh! I didn't use your code, but you gave me an idea. I got it working thanks!

avatar image clunk47 Triqy · Aug 12, 2013 at 02:43 PM 0
Share

Please edit your answer to show your solution, so others can see exactly how you resolved this.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by noooodley · Aug 12, 2013 at 07:44 AM

Is the for loop mandatory? You can easily do what you are describing without a for loop.

As for how for loops work, I would recommend reading the following page, as there is no use in me rewriting what has already been written:

http://www.w3schools.com/js/js_loop_for.asp

Comment
Add comment · Show 3 · 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 Triqy · Aug 12, 2013 at 07:47 AM 0
Share

The for loop is not mandatory. Just looking for the best solution shoot a raycast at the player in 2 second time increments. Any ideas?

avatar image Triqy · Aug 12, 2013 at 07:49 AM 0
Share

I know i can use InvokeRepeating but is there another way to do this loop cycle? maybe in a corontine? or another way?

avatar image noooodley · Aug 12, 2013 at 07:59 AM 1
Share

Something like this would probably work:

 public float timeBetweenShots = 2f;
 private float lastShotTime = 0;
 
 private void Update() {
    //check to see if it is time to shoot
    if (Time.time - lastShotTime > timeBetweenShots) {
       //setup the ray however you need, this is just an example
       Ray myRay = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
       RaycastHit myRayHit;

       //do the raycast
       if (Physics.Raycast(myRay, out myRayHit) {
          //put code here to damage the player
       }
 
       //set 'lastShotTime' to the current time
       lastShotTime = Time.time;
    }
 }

Hope this helps. Does this answer your question?

avatar image
0

Answer by Triqy · Aug 12, 2013 at 08:25 PM

Im not at home right now but here is a rough answer that i came up with...

 var shotTimer: float;    
 var DamagePerSecond = 1;
 var OpenFire = false;
 
 function Update()
 {
     shotTimer = Time.deltaTime * DamagePerSecond;
     
     if(shotTimer > 1)
     {
         OpenFire = true;
         shotTimer = 0;
     }
     
     else
     OpenFire = false;
     
     if(OpenFire == true)
     {
         //Shoot Raycast at Player 
     
 //        if(Raycvast hits player collider)
 //        {
 //            player.playerHealth -= 1;
 //        }
     }
 }

This is rough and not exactly how i did it pertaining to my project because i used triggers to register to see if enemy is within a certain distance. But here is a revised answer!

Comment
Add comment · 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 clunk47 · Aug 29, 2013 at 01:46 AM 0
Share

This doesn't appear to be a feasible solution. Please don't accept something that is not a working resolution. If you still need help, keep the answer open. If you don't wish to post an actual working solution, but have resolved the problem, please simply close the question.

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy damage error 1 Answer

Making bullet holes in enemies. 1 Answer

Enemy to move out of way of player 1 Answer

ApplyDamage Error 0 Answers

Fall Damage Script Problem? 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