- Home /
The question is answered, right answer was accepted
Detect tag in C#
I was confortable in JavaScript making my Aim System, but unfortunately i discovered that i NEED to use C# on this, so i rewrited the script. The problem is that it is the biggest mass i've ever seen. I am a beginner at C# and i am learning about it, so this might be an easy thing but i don't know how to solve it =P
Have you ever seen a box inside a box inside another box? It's basically the same thing here. I want to make a script attached to an aim in which When the player click with the left mouse button, the script will detect if there is something colliding with the aim. If there IS something colliding with the aim, and that thing has the tag "Enemy", then a log will be displayed for testing purposes.
But i ended up making this mess:
using UnityEngine;
using System.Collections;
public class AimSys : MonoBehaviour
{
bool Click; //This is a trigger.
public float fireRate = 0.5F; //Fire rate.
private float nextFire = 0.0F; //Fire rate.
void Update()
{
if (Input.GetButton("Fire1") && Time.time > nextFire) //Shoots on fire rate. I've set it up to 2 seconds before shooting again.
{
Click = true; //If the player has shoot, THEN:
}
}
void OnCollisionEnter(Aim collision) //This is where it gets a mess. This was supposed to detect if there is anything in the aim's collision area and THEN:
{
if (Click) // IF the player has shoot, THEN:
{
if (Aim.gameObject.tag == "Enemy") // Find if the object colliding with the aim is actually an enemy by finding it's tag.
{
Debug.Log("TARGET HIT!"); //This is where i will implement the health reduction.
}
}
}
}
Can someone help me removing some {} and making it actually work? I would appreciate it. Principally by removing some keys or whatever is it called (Not my mother language sorry) This wouldn't happen if i was using Java =C
Answer by jmgek · Nov 04, 2016 at 10:06 PM
using UnityEngine;
using System.Collections;
public class AimSys : MonoBehaviour
{
bool Click = false; //This is a trigger.
bool target = false;
float timer;
void Update()
{
if (target)
if(Input.GetButton("Fire1"))
{
//logic goes here
}
}
void OnCollisionStay(Collision collision) {
{
if(collision.gameobject.name == "Enemy")
target = True;
}
void OnCollisionExit(Collision collision) {
if(collision.gameobject.name == "Enemy")
target = False;
}
}
Ok, i've fixed it. There were some errors on your script and things that doesn't make sense at all...
using UnityEngine;
using System.Collections;
public class AimSys : $$anonymous$$onoBehaviour
{
bool Click = false; //This is a trigger.
bool target = false;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
void Update()
{
if (target)
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
Debug.Log("Target Hit!");
}
}
void OnCollisionEnter(Collision collision)
{
{
if (collision.gameObject.tag == "Enemy")
target = true;
}
}
}
Unfortunatelly it ignores the fire rate.
Sorry wrote it on my Phone:
You might want to keep OnCollisionExit seeing how you're never setting target to false.
You're also going to want to put a timer somewhere in your Update, I am subtracting but you can do whatever you want.
if(nextfire >= 0)
nextFire -= time.deltaTime;
if (Input.GetButton("Fire1") && nextFire <= 0)
{
Debug.Log("Target Hit!");
nextFire = fireRate;
}
It's not the cleanest but it should work, You can easily find fire mods online to do a clean job.
Ohh i forgot that part! It worked perfectly now, thanks! ^.^