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 Shmks · Aug 09, 2014 at 06:17 PM · collisionraycasttimerraycasthittime.deltatime

Raycast and timer to select and lock-on to targets

I'm creating a mechanic (like a fighter jet simulation) where the player holds down the fire button and locks on to enemies. When the crosshair is on an enemy, the target is selected, if he keeps the crosshair on the enemy for a set amount of time (lock-on time) the target is added to an array (lockedTargets). I'm having trouble.

More EDITS!

Here is the current code:

 public class TargetAttack : MonoBehaviour {
     
     public float attackRange = 7f;
     public float lockTime = 2.0f;
     public int numLocks = 0;
     public int maxLocks = 5;
     public GameObject currentTarget = null;
 
     public string currentTargetName;
     public float running_time = 0f;
 
     // Use this for initialization
     void Start () 
     {
         running_time = 0f;
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetButton ("Fire1")) 
         {
             EyeTarget ();
         }
     }
     
     void EyeTarget ()
     {
 
         Ray ray = Camera.main.ScreenPointToRay (new Vector2 ((Screen.width / 2), (Screen.height / 2)));
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit, attackRange)) 
         {
             if(hit.collider.gameObject.tag == "Enemy")
             {
                 currentTarget = hit.collider.gameObject;
                 currentTargetName = hit.collider.name;
                 //Debug.Log(hit.collider.name);
                 Debug.DrawLine (ray.origin, hit.point);
 
                 running_time += Time.deltaTime * 1;
 
                 if( running_time >= lockTime && numLocks < maxLocks)
                 {
                     numLocks++;
                     Debug.Log("Locked on");
                     running_time = 0f;
 
                 }
             }
             
             else            
             {
                 ResetTimer();
                 running_time = 0f;
 
             }
         }
     }
     
     void ResetTimer()
     {
         running_time = 0f;
         Debug.Log("resetting timer");
     }
 }
 


1) The lock-on timer. I got the timer to work as I'd like, though I'd like it to stop counting after all the lock-ons are acquired. It doesn't.

2) The timer only resets on collision. This is still an issue. I want the timer to reset when there is no collision with anything, because that means the player has stopped tracking the target, or the target has moved out of range. However, in the current implementation, the timer only resets when I hit the floor with the ray (because it collides but it is not tagged "enemy"). When I shoot off into the sky, the timer stops but remains where it was. Which means it can be resumed if I bring the cursor back onto an enemy. How can i get it to reset by default and count only on contact?

3) Also it seems like I don't need to have an if loop for line 36 (" if (Physics.Raycast (ray, out hit, attackRange)) ") but when I simply use Physics.Raycast (ray, out hit, attackRange); I get odd errors when targeting ("

NullReferenceException: Object reference not set to an instance of an object TargetAttack.EyeTarget () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/TargetAttack.cs:38) TargetAttack.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/TargetAttack.cs:27)

"). I think there's something wrong with my order of operations or the loop type I'm using but i'm too much of a noob to figure out whats going on.

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 christoph_r · Aug 10, 2014 at 05:41 PM

  1. Check in if(hit.collider.gameObject.tag == "Enemy") also if the max number of lock-ons is reached.

  2. Call your ResetTimer also in an else order (after the Raycast check). So if there's no collision, it will reset the timer.

  3. No, that piece of code makes sense. And it's not a loop anyway, it's just a single check that gets called every frame (due to being called in Update() ).

Hope that helps.

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 Shmks · Aug 10, 2014 at 05:51 PM 0
Share

Thanks! Your comments are spot on. I made the changes and it works great! Just for those that may need something similar, the working code is:

 void EyeTarget ()
     {
 
         Ray ray = Camera.main.ScreenPointToRay (new Vector2 ((Screen.width / 2), (Screen.height / 2)));
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit, attackRange)) {
                 Debug.DrawLine (ray.origin, hit.point);
 
                 if (hit.collider.gameObject.tag == "Enemy" && numLocks < maxLocks) {
                         currentTarget = hit.collider.gameObject;
                         currentTargetName = hit.collider.name;
                         //Debug.Log(hit.collider.name);
                         Debug.DrawLine (ray.origin, hit.point);
 
                         running_time += Time.deltaTime * 1;
 
                         if (running_time >= lockTime && numLocks < maxLocks) {
                                 numLocks++;
                                 Debug.Log ("Locked on");
                                 running_time = 0f;
 
                         }
                 }
         }
         else 
         {
             ResetTimer ();
             running_time = 0f;
             currentTarget = null;
         }
     }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Fall collision force with help of raycast 0 Answers

Raycasting to detect which side of collider is hit 2 Answers

Detect RaycastHit on Character Controller 2 Answers

Raycast Not Drawing In Target Direction 0 Answers

Raycast bullet collision 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