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 Danteeva · Jan 13, 2014 at 08:31 PM · c#enemyclicktargeting

Targeting script not finding a target 2D Mode

Hello, I am trying to get it so that when you click on a enemy sprite component with the tag "Enemy" it selects it as a target. I have tried several different methods, but I dont have the coding knowledge yet to figure it out. I have found this script on here but when I click on an enemy (It is tagged as Enemy) nothing happens and nothing is selected. Could anyone tell me why this is happening and how to fix it please?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class Targeting : MonoBehaviour {
     
     public Transform selectedTarget;
     
     void Update(){
         if (Input.GetMouseButtonDown(0)){ // when button clicked...
             RaycastHit hit; // cast a ray from mouse pointer:
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             // if enemy hit...
             if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Enemy")){
                 DeselectTarget(); // deselect previous target (if any)...
                 selectedTarget = hit.transform; // set the new one...
                 SelectTarget(); // and select it
             }
         }
     }
     
     private void SelectTarget(){
         selectedTarget.renderer.material.color = Color.red;
         Debug.Log ("Enemy Targeted");
     }
     
     private void DeselectTarget(){
         if (selectedTarget){ // if any guy selected, deselect it
             selectedTarget.renderer.material.color = Color.blue;
             selectedTarget = null;
         }
     }
 }
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 robertbu · Jan 13, 2014 at 09:06 PM

You will not be able to use a 3D raycast in a 2D situation. Here is a bit of a rewrite to the code you are posting so that it will detect 2D colliders. I'm assuming your 2D objects are Sprites, so I've changed the way the color is changed. If they are not sprites, you can uncomment the lines for the original way these objects were colored.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class Targeting : MonoBehaviour {
     
     private Transform selectedTarget = null;
     
     void Update(){
         if (Input.GetMouseButtonDown(0)){ // when button clicked...
             Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             Debug.Log (pos);
             RaycastHit2D hit; // cast a ray from mouse pointer:
             hit = Physics2D.Raycast (pos, Vector3.zero);
 
             if (hit != null && hit.transform != null && hit.transform.CompareTag("Enemy")) {
                 DeselectTarget(); // deselect previous target (if any)...
                 selectedTarget = hit.transform; // set the new one...
                 SelectTarget(); // and select it
             }
         }
     }
     
     private void SelectTarget(){
         selectedTarget.GetComponent<SpriteRenderer>().color = Color.red;
         // selectedTarget.renderer.material.color = Color.red;
         Debug.Log ("Enemy Targeted");
     }
     
     private void DeselectTarget(){
         if (selectedTarget != null){ // if any guy selected, deselect it
             selectedTarget.GetComponent<SpriteRenderer>().color = Color.blue;
             //selectedTarget.renderer.material.color = Color.blue;
             selectedTarget = null;
         }
     }
 }
Comment
Add comment · Show 5 · 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 Danteeva · Jan 13, 2014 at 11:28 PM 0
Share

Thanks for the reply. I tried it, but nothing happened. It posts the click position in the debug, but it doesn't find a target. To be honest, I don't understand the colour bit either. Does it matter that the are all along the same Z axis or not, as I cant seem to get sorting layers on them to work either, the enemy sprite is always in front of the player no matter how i set it.

avatar image robertbu · Jan 13, 2014 at 11:59 PM 0
Share

I tested this code with Sprites before I posted it, so I know it works:

  • $$anonymous$$ake sure your sprites have colliders

  • $$anonymous$$ake sure your sprites have the tag 'Enemy'

avatar image Danteeva · Jan 14, 2014 at 12:09 AM 0
Share

Ah, that probably the problem then, which collider should I use, I've tried 2D rigid and 2d box, but still nothing :/

avatar image robertbu · Jan 14, 2014 at 12:18 AM 0
Share

I tested the code with a couple of 2D box colliders, and it worked fine. So I'm not sure what to tell you. You need to add some Debug.Log() statements to see what is going on. Start by checking 'hit' after the Raycast() call to see if you are hitting anything. Take out the 'hit.transform.CompareTag("Enemy")' to test if you have a tag issue or not.

avatar image Danteeva · Jan 14, 2014 at 12:35 AM 0
Share

Ah got it working, didn't set the collider properly. I'm such a newb. Thanks again! I'm sure I'll be back again for something else another day -.-

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

18 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

Related Questions

Multiple Cars not working 1 Answer

How can I make my player only target objects from a certain distance? 1 Answer

Distribute terrain in zones 3 Answers

Implement moveSpeed to this object script? 1 Answer

lookat wont track target 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