I am trying to cast a circle at my mouse position to detect multiple objects at the same time but this code gives me errors(Can’t convert type ‘UnityEngine.Ray’ to ‘UnityEngine.Vector3’)! thanks for the help and suggestion to do it in a different way!
void CheckCirclecast()
{
Vector2 worldPosition = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hitData = Physics2D.CircleCast(worldPosition, 0.1f, Vector2.zero);
if (hitData && Input.GetMouseButtonDown(0))
{
Debug.Log(hitData);
}
}
iohouse
November 29, 2021, 2:49pm
3
It looks like you’re using the wrong overload for Physics2D.CircleCast()… try the below code where it uses a list for the objects hit by the raycast :
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var hits = new List<RaycastHit2D>();
var results = Physics2D.CircleCast(worldPosition, 1f, Vector2.zero, new ContactFilter2D().NoFilter(), hits);
if (results > 0)
{
Debug.Log($"{results} colliders hit!");
}
foreach (var item in hits)
{
Debug.Log($"Hit '{item.collider.name}'!");
}
}
}
Ref. https://docs.unity3d.com/ScriptReference/Physics2D.CircleCast.html
You are doing it head wall
Try making a collider and make it a trigger than in run time resize it and call function
OnTriggerEnter(Collider col){
Debug.Log(col.name);
}
transform.translate.position = hitData.position;