Howdy folks! I have a bit of a issue that has me super stumped here. I am going from Key codes , to mouse input - then it later become touch input, but first I want to figure out why i cant do this with just a mouse.
I have a raycast2d setup - and I want the raycast to read collision with my objects on the screen. They are to only react if its an object tagged as “Cat” - essentially once that happens, the cat will swipe out and try to attack. However, it tells me the tagg itself is a reference that instantiated. But the object itself exists by default so im not sure what to do here. Here is my whole script.
using System.Collections;
using UnityEngine;
public class PlayerAttack : MonoBehaviour {
private bool attacking = false;
private float attackTimer = 0;
private float attackCd = .2f;
public Collider2D attackTrigger;
public GameObject catObject;
private Animator anim;
void Awake() {
anim = gameObject.GetComponent<Animator>();
attackTrigger.enabled = false;
}
void Update() {
if (Input.GetMouseButtonDown(0) && !attacking && attackTimer <= 0 && (tag == "cat")) { //every frame check to see if the mouse has been clicked.
//Get the mouse position on the screen and send a raycast into the game world from that position.
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);//Vector 2 means only 2 points of axis are read. worldpoint means check the point of the world. Camera is used to determien what we are looking at, and we fill it with our mouse location.
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
if (hit.collider.tag == "Cat") {//THIS IS THE LINE that says it is having issues. If i REMOVE this line, ANYWHERE i click on the screen activates the cat and thats not what I want to happen.
Debug.Log("Touched it");
attacking = true;
attackTimer = attackCd;
attackTrigger.enabled = true;
{
}
if (attacking) {
if (attackTimer > 0) {
attackTimer -= Time.deltaTime;
} else {
attacking = false;
attackTrigger.enabled = false;
}
}
anim.SetBool("Attacking", attacking);
}
}
}
}
So, the way the script is set, is within the object itself. Rather than a raycast manager of some sorts - because I have 4 cats on the screen, and each should be selected to attack per tap. I wasnt sure how to do this effectively. So the ray cast should read when i go out and poke anything with a cat tag, then activate the cat. I will have diff tags for diff cats, so cat, cat1, cat2, cat3. and each script will relate to the tag seperately. Is there any other way I can try doing this? What am i missing? If you need more scripts let me know.