Kris
Kris

Reputation: 19

Raycast help (using C#)

I have a raycast going upwards from an object which when the player comes in contact with the ray the object changes color. That works but I want to do it so when you touch the ray a second time, the object gets destroyed and I have no idea how to do that. I'm using Unity 2d.

Code: `using System.Collections; using System.Collections.Generic; using UnityEngine;

public class DestroyEnemy : MonoBehaviour //Enemy 3 {

[SerializeField] Transform Enemy3WayPoint;
private Renderer rend;
private Color colorToTurnTo = Color.blue;



void Start()


{
    rend = GetComponent<Renderer>();
    rend.enabled = true;
    Physics2D.queriesStartInColliders = false;
}

private void Update() {

    RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector3.up, 5);
    if (hitInfo.collider.gameObject.tag == "Player")
    {

        rend.material.color = colorToTurnTo;
       Debug.DrawLine(transform.position, hitInfo.point, Color.white);


    }`

There may be a bracket or two I forgot to include, it does work when I test it

Upvotes: 0

Views: 236

Answers (1)

user2955146
user2955146

Reputation: 351

I think the simplest solution is to use a variable to keep track of the number of times the ray has been hit by the player.

As for destroying the enemy, you can use the destroy function. So, something like this:

int hitCount = 0; //add a class variable

void Update(){

    RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector3.up, 5);

    if (hitInfo.collider.gameObject.tag == "Player")
    {
        hitCount++;
    }

    if(hitCount == 1)
    {
        rend.material.color = colorToTurnTo;
        Debug.DrawLine(transform.position, hitInfo.point, Color.white);
    }
    else if(hitCount >= 2)
    {
        Destroy(gameObject); //this will destroy the gameObject that the component is attached to 
    }
}

EDIT: It seems the OP's main problem was adding a delay to the events. Here is some updated code that addresses that problem:

bool waitingForFirstHit = true;
bool waitingForSecondHit  = false;
float timeDelay = 1.5f; 

void Update(){

    RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector3.up, 5);

    if (hitInfo.collider.gameObject.tag == "Player" )
    {
        if (waitingForFirstHit) {

            ChangeColor();          
            waitingForFirstHit = false;
            waitingForSecondHit = true;
        }           
        else if(waitingForSecondHit && timeDelay < 0)
        {
            DestroyEnemy ();
        }
    }


    if(waitingForSecondHit)
    {
        timeDelay -= Time.deltaTime;
    }
}

void ChangeColor()
{
    rend.material.color = colorToTurnTo;
    Debug.DrawLine(transform.position, hitInfo.point, Color.white);
}

void DestroyEnemy()
{
    Destroy(gameObject);
}

Here is a tutorial on using the Destroy function: https://unity3d.com/learn/tutorials/topics/scripting/destroy

And here is a link to the docs: https://docs.unity3d.com/ScriptReference/Object.Destroy.html

Cheers.

Upvotes: 0

Related Questions