• 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
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 $$anonymous$$t;
             
             if( Physics.Raycast( ray, out $$anonymous$$t,  100 ) )
             {
                 float distance = Vector3.Distance($$anonymous$$t.transform.position, transform.position);
 
                 
                 if(distance < 8) {
                 string $$anonymous$$tTag = $$anonymous$$t.transform.tag;
                 if ($$anonymous$$tTag == "enemy" && newon == true)
                 {
                 Debug.Log( $$anonymous$$t.transform.gameObject.name );
                 target = $$anonymous$$t.transform.gameObject;
                     SelectTarget();
                 }
                 }
 
             }
         }
 
 
         if (Input.GetMouseButtonDown (1)) {
                 DeselectTarget();
                 }
Comment

People who like this

0 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
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

People who like this

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Raycast target sight? 0 Answers

Follow Nearest Target 1 Answer

How can i chose the previous target 2 Answers

spherecast wont work 2 Answers

Disable a target after trigger exit? 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