Hello Everybody . I have a code that is used to put some buildings in the ground .
I want my towers to be red , when they cant be placed and green when it is allowed . There is only one problem tho . My program stops working as soon as I implement Physics.raycast with distance . Let me show you what I mean .
“”"
private void HandlePlacemet()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log(“ShootingRay”);
if (Physics.Raycast(ray, out var hitInformation))
{
objectToPlace.transform.position = hitInformation.point;
objectToPlace.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInformation.normal);
}
}
“”"
Code Like this is working fine , it is doing just exactly what you would think . It gets some points , and then it transforms the object to those points . Simple. However this seems to break everything .
“”"
private void HandlePlacemet()
{
//Lets say that the layerMask is 1<< 8; Doesnt matter . It is not the problem.
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log(“ShootingRay”);
if (Physics.Raycast(ray, out var hitInformation,Mathf.Infinity,_layerMask))
{
objectToPlace.transform.position = hitInformation.point;
objectToPlace.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInformation.normal);
}
}
“”"
As soon as I imp[element the Distance , doesn’t matter what distance (10, 1000,1) it breaks.
It worked with 10 , I believe if I remember correctly but then it just spawnd the buildings in one place …
Any ideas ??? Very interesting problem