- Home /
2D Raycast effect only what it hits?
Hell again! I am having an issue with Raycast, the scripts is telling unity to "kill" all game objects with the tag "enemy" once a bool triggers to true. It triggers true once the Raycast collides with an enemy tagged game object.
But I need it so only the enemy the raycast is hitting will run the script, and be "killed". So how do I check to see if something is being hit by the raycast and apply that to only one of the prefabs, and not all of them.
This is the Raycast shooting script
public Transform sightStart, sightEnd;
public bool spotted = false;
public bool isBeingHit = false;
void Update() {
Raycasting ();
damage ();
}
void Raycasting()
{
Debug.DrawLine (sightStart.position, sightEnd.position, Color.red);
spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("enemy"));
}
void damage() {
if(Input.GetKey (KeyCode.Mouse0)) {
if(spotted == true) {
isBeingHit = true;
}else {
isBeingHit = false;
}
}
}
is works and once it sees a tagged enemy it triggers "spotted" to true. and once the player presses the mouse button "isBeingHit" also triggers to true, this triggers another script to kill the enemy.
public playerShooting shoot;
public Animator enemyAnim;
public Collider2D col;
void Start() {
col = GetComponent<BoxCollider2D> ();
enemyAnim = GetComponent<Animator> ();
shoot = GameObject.FindGameObjectWithTag("player").GetComponent<playerShooting> ();
}
void Update() {
if(shoot.GetComponent<playerShooting>().isBeingHit == true) {
enemyAnim.SetBool ("isDead", true);
col.enabled = false;
}
}
I have this on my enemy prefabs, that once trigged kills the enemy. But as expected it kills all of them even though they are not the one with the raycast on them. It is doing what it is being told to do by the script.
I just can't figure it out on how to make so only the one that is being hit by the raycast is the one to be killed, and not all of the prefabs on the map.
I hope this makes sense!
Thank for any help!
Answer by alexi123454 · Jul 23, 2015 at 07:05 PM
The reason it's killing all of them is because you put the "spotted" variable on the wrong object! When the raycast hits an enemy, instead of setting "spotted" to true on the player, each enemy object should have a "spotted" variable that is being set to true (if the player sees someone, the player isn't spotted, the ENEMY is).
Change the scripts to something like this:
public Transform sightStart, sightEnd;
public bool isBeingHit = false;
void Update() {
Raycasting ();
damage ();
}
void Raycasting()
{
Debug.DrawLine (sightStart.position, sightEnd.position, Color.red);
RaycastHit2D hit = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("enemy"));
if (hit)
{
hit.transform.GetComponent<EnemyScriptName>().spotted = true;
}
}
void damage() {
if(Input.GetKey (KeyCode.Mouse0))
{
isBeingHit = true;
}
else
{
isBeingHit = false;
}
}
and this:
public playerShooting shoot;
public Animator enemyAnim;
public Collider2D col;
public bool spotted = false;
void Start() {
col = GetComponent<BoxCollider2D> ();
enemyAnim = GetComponent<Animator> ();
shoot = GameObject.FindGameObjectWithTag("player").GetComponent<playerShooting> ();
}
void Update() {
if(shoot.GetComponent<playerShooting>().isBeingHit == true && spotted)
{
enemyAnim.SetBool ("isDead", true);
col.enabled = false;
}
else
{
spotted = false;
}
}
Thank you $$anonymous$$i! why did I not notice that, I was thinking it was going to be some complicated process. I have done it 100s of time in the past too. Thank you very much!
Answer by nisovin · Jul 23, 2015 at 07:06 PM
It doesn't really make sense to have the isBeingHit variable on a component attached to the player object. Rather, it should be on a component attached to the enemy object.
Now, the Physics2D.Linecast method actually returns a RaycastHit2D object. You can treat it as a boolean, but you can also use it to get the collider of the object that was hit. So, assuming you've moved isBeingHit to your enemy component, and assuming your enemy component is called "enemy", then you can make your code look like this:
public Transform sightStart, sightEnd;
public RaycastHit2D spotted;
void Update() {
Raycasting ();
damage ();
}
void Raycasting()
{
Debug.DrawLine (sightStart.position, sightEnd.position, Color.red);
spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("enemy"));
}
void damage() {
if(Input.GetKey (KeyCode.Mouse0)) {
if(spotted != null) {
spotted.collider.GetComponent<enemy>().isBeingHit = true;
}
}
}
Now in your enemy script you can remove the reference to the player and just check its own isBeingHit variable.
public bool isBeingHit;
public Animator enemyAnim;
public Collider2D col;
void Start() {
col = GetComponent<BoxCollider2D> ();
enemyAnim = GetComponent<Animator> ();
}
void Update() {
if(isBeingHit) {
enemyAnim.SetBool ("isDead", true);
col.enabled = false;
}
}
You could also probably do a bit of cleanup and code efficiency changes as well, like moving the Linecast into the damage() method, since you only need to do that if the mouse button is pressed. However, I've tried to leave your code as similar as possible to the way you had it.
Thank you Nisovin, don't know why I did not figure it out. I will take your advice and clean up like you said. Thank you
Your answer
Follow this Question
Related Questions
Layer Mask on raycast2d not working? 1 Answer
Raycast for a 2D objects 0 Answers
Raycast2D will point right, but won't point left when player turns. 1 Answer
Error with 2D Raycast 0 Answers