I want to make a raycast from the player position to the mouse position but it should only have a certain range.
I have tried the following:
using UnityEngine;
public class Raycasting : MonoBehaviour
{
public GameManager gm;
Vector3 worldPosition;
public Transform player;
void FixedUpdate()
{
//Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
Debug.DrawLine(player.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), Color.green);
RaycastHit2D hit = Physics2D.Raycast(player.transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), 10f);
if(hit.collider.tag == "Enemy")
{
Debug.Log (hit.collider.gameObject);
gm.Attack();
if (GameManager.enemyhealth <= 0)
{
Debug.Log("Enemy Died!");
Destroy(hit.transform.gameObject);
}
}
}
}
in Debug.DrawLine()
it works exactly as I want it to — without the range —, but the raycast dosen't detect the enemies around it.