• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How can I apply damage based on a grenade explosion, but test whether the objects are behind cover so they are not damaged even within the explosion radius? 2 Answers

Follow Nearest Target 1 Answer

How to make mobile controls shoot prefab bullets 0 Answers

Player's projectile communicating with Enemy 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges