'Raycast from the player position to the mouse position within range
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.
Solution 1:[1]
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..");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
