- Home /
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;
}
}
}
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;
}
}
}
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.
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'
Ah, that probably the problem then, which collider should I use, I've tried 2D rigid and 2d box, but still nothing :/
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.
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
Follow this Question
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