Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Omerl_ahire · Dec 24, 2017 at 12:11 AM · raycastingraycasthitelse

Raycast if no hit problems

Hi, I'm trying to make a script that locks on to a target if it is hit by the ray for a certain period of time, while the script works for the most part, if the raycast hits something the "locking" variable does not get turned to true. I think the "Locking" variable is being overwritten by the else function that is meant to set it to false.

alt text

this GIF shows what happens if I have the locking set to false in the raycast else statement.

alt text this Gif shows what happens if I remove the locking bool from the raycast else statement

     void Update() {
         {
             RaycastHit hit;
             if (Physics.Raycast(transform.position, gameObject.transform.forward, out hit, 1000)) {
                 ArmLeft.transform.LookAt(hit.point);
                 ArmRight.transform.LookAt(hit.point);
                 Debug.DrawLine(gameObject.transform.position, hit.point, Color.yellow);
                 locking = true;
                 ///////////////Lockon Setting//////////////////
          //       #region LockOn
 
                 if (hit.collider.gameObject.tag == "Mech") {
 
                     foundTarget = hit.collider.gameObject;
                 }
                 else
                     foundTarget = null;
             }
             else
                 ///////This is what I want to happen if the ray doesn't hit///////////
                ArmLeft.transform.LookAt(nullRayHit);
                ArmRight.transform.LookAt(nullRayHit);
             locking = false;
         }
         ////////these control the setting and unsetting the locked target///////
         if (foundTarget != null) {
             lostTime = startLostTime;
             lockTime -= Time.deltaTime;
         }
         else
             lockTime = startLockTime;
             lostTime -= Time.deltaTime;
 
         if(lockTime <=0 ) {
             lockedTarget = foundTarget;
         }
         if (lostTime <= 0) {
             lockedTarget = null;
         }
 //#endregion

thanks for your help.

mech-lock1.gif (395.4 kB)
mech-lock2.gif (331.1 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
1
Best Answer

Answer by Larry-Dietz · Dec 24, 2017 at 05:03 AM

Your problem is in your else clause.

Right now the only thing in else is ArmLeft.transform.LookAt(nullRayHit);

You are missing { }'s around your else clause, so only the first line is being used in else, and the next two lines which should be in else are executing every time.

Just wrap the lines for your else clause in { }'s and you should be good.

Also, just to point it out, you have another else clause near the bottom of the code with the same issue.

Hope this helps, -Larry

Comment
Add comment · Show 2 · 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 Omerl_ahire · Dec 24, 2017 at 07:21 AM 0
Share

Thanks for your answer, the scipt is working perfectly now.

avatar image Larry-Dietz Omerl_ahire · Dec 24, 2017 at 11:35 PM 0
Share

Glad I could help!

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

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

Related Questions

Raycasting problem, colliders not being recognized? 1 Answer

RayCast appears to bug out for no apparent reason 1 Answer

Raycast help 0 Answers

Switching from object to object with Raycast Problem 1 Answer

Raycast doesn't stay in one place 0 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