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 convictcartel · Jan 04, 2014 at 10:15 PM · raycastenemychangetarget

Ray Cast Click on Enemy - Change Current Target

My raycast target enemy works perfect, my only problem is when I have a target the only way to untarget is to kill the enemy or to right click. I want to make it so if I have a target and i choose to click another one, the target will change. Cant figure it out!

 void Update () {
 
 
         if( Input.GetMouseButtonDown(0) && newon == true)
         {
 
             Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
             RaycastHit hit;
             
             if( Physics.Raycast( ray, out hit,  100 ) )
             {
                 float distance = Vector3.Distance(hit.transform.position, transform.position);
 
                 
                 if(distance < 8) {
                 string hitTag = hit.transform.tag;
                 if (hitTag == "enemy" && newon == true)
                 {
                 Debug.Log( hit.transform.gameObject.name );
                 target = hit.transform.gameObject;
                     SelectTarget();
                 }
                 }
 
             }
         }
 
 
         if (Input.GetMouseButtonDown (1)) {
                 DeselectTarget();
                 }
Comment
Add comment · Show 3
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 ShadoX · Jan 04, 2014 at 11:30 PM 0
Share

Sorry, it's hard to understand if your having trouble changing targets or just de-selecting the chosen target. If you'd like to just get rid of the variable assigned to "target", then why not just do..

 target = null; 
avatar image DiligentGear · Jan 04, 2014 at 11:59 PM 0
Share

I don't know what that "newon" does, but it looks like some function falsely modified "newon" variable, causing it doesn't raycast at all.

avatar image convictcartel · Jan 05, 2014 at 03:37 AM 0
Share

newon just is a made up variable that checks if a target is selected or not.. and i want to make it so if a target is selected when I click a differnt game object that game object becomes the new target

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by EHogger · Jan 05, 2014 at 04:34 AM

If "newon" is to check that a target is already selected, then if you want to be able to select a new target anyway all you have to do is remove the if newon = true part.

Depending on what those functions do you may also need to add DeselectTarget() on the line before SelectTarget()

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 EHogger · Jan 05, 2014 at 04:39 AM 0
Share

Just as an aside, youre checking for newon = true twice in the same if statement. Lines 4 and 17 on here. Since that second one is nested within the first if statement it isn't needed anyway.

avatar image convictcartel · Jan 05, 2014 at 09:50 PM 0
Share

Lmao Oh. Duh! Works perfect now! Thanks a bunch!

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

21 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

Related Questions

Follow Nearest Target 1 Answer

Raycast target sight? 0 Answers

Enemy does not look the player 2D platformer. 1 Answer

Look at enemy 1 Answer

Hitmarker appears on both targets 2 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