I’m trying to make a simple fighting game.
I figured I’d use Raycast to check if the opponent is in range, but encountered a problem. My Raycast can’t detect the other player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class player_1 : MonoBehaviour {
public float reach = 2;
public bool inRange = false;
void FixedUpdate ()
{
RaycastHit hit;
Ray punchRay = new Ray (transform.position, Vector3.right * reach);
Debug.DrawRay (transform.position, Vector3.right * reach);
if (Physics.Raycast (punchRay, out hit, reach)) {
if (hit.collider.gameObject.tag == "player_2")
inRange = true;
}
}
}
Hope you can help.