-1

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Welcome to Stack Overflow. Unfortunately, "doesn't seem to work" is a *really* vague description of what you're seeing - please read https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ and edit your question accordingly. – Jon Skeet May 21 '22 at 11:04
  • 1
    _"Its not letting me add the title its about raycasts2D"_ - rather than fighting the Stack Overflow **Ask Question** screen, instead follow the friendly on-screen prompts such as _Be specific and imagine you’re asking a question to another person_. [ask] –  May 21 '22 at 11:08
  • 1
    Welcome to SO, just know that "urgency" doesn't exist on this site, and it is good that you haven't mentioned this in the question itself. The best way to get quick answers though, is to write a damn good question, the absolute best one possible. – Hovercraft Full Of Eels May 21 '22 at 11:11
  • Sorry I will write questions better next time but could you please help me? – Arbalistic_Cow May 21 '22 at 11:13
  • Are you using the C# programming language? Which language are you using (as you only mention the library)? This should be one of your question tags so as to attract the experts in that question. – Hovercraft Full Of Eels May 21 '22 at 11:25
  • yes i am using c# – Arbalistic_Cow May 21 '22 at 11:26
  • 1
    [tag:c#] tag added. Again, best to do this to get the right experts to your question – Hovercraft Full Of Eels May 21 '22 at 11:29
  • 1
    "Sorry I will write questions better next time" - edit *this question* to be a better one. You don't seem to understand: the better the question is, the more likely you are to get an answer (and the quicker that's likely to happen). Now, what *exactly* do you mean by "doesn't detect the enemies around it"? (Given `GameManager.enemyhealth` that suggests you've only got a single enemy...) Have you examined `hit` in the debugger and compared that with what you expect? – Jon Skeet May 21 '22 at 11:35

1 Answers1

0

There is a trick to getting the ray end point to solve your problem. Just make sure your camera is orthographic. Also, by determining the enemy layer, detection problems are eliminated.

public GameObject player;
public LayerMask enemyLayer;
void Update()
{
    var point = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(1);

    point.z = player.transform.position.z;
        
    Debug.DrawLine(player.transform.position, point);

    var enemy = Physics2D.Linecast(player.transform.position, point, enemyLayer.value);

    if (enemy)
    {
        // do something...
    }
}

Also, if you want to control the distance, please leave a comment.


Limited Distance to pointer

This algorithm limits the distance. For example, if you enter 5 for distance, the maximum magnitude will be 5, and if the mouse approaches below 5, it will set the mouse point to the maximum.

public GameObject player;
public LayerMask enemyLayer;
public float distance = 4.5f;

void FixedUpdate()
{
    var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    point.z = player.transform.position.z;

    var clamp = Vector3.ClampMagnitude(point - player.transform.position, distance);
    
    Debug.DrawLine(player.transform.position, player.transform.position+clamp);

    var enemy = Physics2D.Linecast(player.transform.position, player.transform.position+clamp, enemyLayer.value);
    
    if (enemy)
    {
        Debug.Log("Detected..");
    }
}

Fixed Distance along pointer direction

This algorithm takes the mouse to the player and then adds the size. Mouse location does not affect size and distance is fixed.

public GameObject player;
public LayerMask enemyLayer;
public float distance = 4.5f;

void FixedUpdate()
{
    var point = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(1);

    point.z = player.transform.position.z;

    var direction = (point - player.transform.position).normalized;

    Debug.DrawRay(player.transform.position, direction*distance);

    var enemy = Physics2D.Raycast(player.transform.position, direction, distance, enemyLayer.value);
    
    if (enemy)
    {
        Debug.Log("Detected..");
    }
}
KiynL
  • 4,097
  • 2
  • 16
  • 34