Physics Raycast not working
My english is bad Im sorry :(
Hi everyone, I make a building system. But it is not working. If I'm enable collider gameobject is come towards camera. But if I'm disable collider system is working but not colliding with walls or other gameobejcts.
My video: https://www.youtube.com/watch?v=uydj25ER4ik&feature=youtu.be
public float grid = 0.25F;
public GameObject anlikItem;
public Material green;
public Material red;
public Material defaultMaterial;
public bool isBuildMode;
public float degree = 90;
void Update () {
if (isBuildMode)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (anlikItem.GetComponent<DekorItem>().isInterior)
{
if (Input.GetButtonDown("Click"))
{
float x = Mathf.Round(hit.point.x / grid) * grid;
float z = Mathf.Round(hit.point.z / grid) * grid;
anlikItem.transform.position = new Vector3(x, hit.point.y, z);
anlikItem.GetComponent<Renderer>().material = defaultMaterial;
anlikItem.GetComponent<BoxCollider>().enabled = true;
anlikItem.GetComponent<NavMeshObstacle>().enabled = true;
isBuildMode = false;
}
else
{
anlikItem.GetComponent<Renderer>().material = green;
}
}
else
{
anlikItem.GetComponent<Renderer>().material = red;
}
if (Input.GetButtonDown("Rotate"))
{
anlikItem.transform.Rotate(Vector3.up, degree);
}
anlikItem.transform.position = new Vector3(Mathf.Round(hit.point.x / grid) * grid, hit.point.y, Mathf.Round(hit.point.z / grid) * grid);
}
}
}
//Button clicked event..
public void SetBuildModeTrue()
{
anlikItem = Instantiate(GameObject.Find("Control").GetComponent<Control>().selectedItem);
anlikItem.SetActive(true);
defaultMaterial = anlikItem.GetComponent<DekorItem>().defaultMat;
isBuildMode = true;
}
Answer by aldonaletto · Jun 24, 2017 at 03:50 AM
The problem is the anlikItem object's collider: as soon as the object is created, the raycast hits it instead of the ground, and the grid system makes the object be positioned closer to the camera update after update. There are two main solutions, both in SetBuildModeTrue():
1- Place the object in the Ignore Raycast layer (layer 2 - default layer is 0):
...
defaultMaterial = anlikItem.GetComponent<DekorItem>().defaultMat;
anlikItem.layer = 2; // place object in Ignore Raycast layer
isBuildMode = true;
}
2- Or make sure the collider is disabled:
...
defaultMaterial = anlikItem.GetComponent<DekorItem>().defaultMat;
anlikItem.GetComponent<BoxCollider>().enabled = false; // disable object's collider
isBuildMode = true;
}
I try first code. Works well. I try collision anlikItem and wall with this code but it is not working..
private void OnCollisionEnter(Collision collision)
{
Debug.Log(collision.gameObject.tag);
}
Your answer
Follow this Question
Related Questions
RayCast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask) question 0 Answers
Player in front of enemy check not working, Physics.Raycast()? 1 Answer
Raycast doesn't stay in one place 0 Answers
Accessing Raycast collider from another script 0 Answers
Raycast hit.point differs from Ray direction [SOLVED] 1 Answer