- Home /
Question by
oren_danniel · Aug 17, 2017 at 02:11 PM ·
physicsraycaststandalone
Unity3D standalone: raycast doesn't work correctly
Hello everyone, I am currently building a vehicle building game, where the player can place or remove blocks using raycast. When I run the scene in the editor it works splendidly however when I build the project and run the scene sometimes it works correctly sometimes it doesn't cast the ray and sometimes it instantiate the block on another block.
The raycast range is set to 50. The main block has a box collider + rigidbody and the other blocks have just box collider and they are the children of the main block
Can some one give a hand?
if (Input.GetMouseButtonDown(1)) {
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray2, out hit, 10000)) {
con.text += hit.collider.transform.name + Range;
if (hit.collider.transform.tag == "part") {
Destroy(hit.collider.transform.gameObject);
}
}
}
if (Input.GetMouseButtonDown(0)) {
RaycastHit hitPoint;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitPoint, Range)) {
con.text += hitPoint.collider.transform.name + Range;
bool trigger = false;
if (hitPoint.collider.transform.GetComponent<partScript>() != null) {
trigger = !hitPoint.collider.transform.GetComponent<partScript>().unplacableObject;
}
if (trigger || hitPoint.collider.transform.GetComponent<CPU>() != null) {
GameObject tmp = Instantiate(Cube, hitPoint.normal + hitPoint.collider.transform.position, Quaternion.identity);
tmp.transform.eulerAngles = rotations;
roundPosition(tmp);
if (tmp.GetComponent<partScript>().overwriteRotation) {
rotate(tmp.GetComponent<partScript>(), hitPoint, false);
}
}
}
}
UPDATE
untitled.png
(333.8 kB)
Comment
Best Answer
Answer by oren_danniel · Aug 17, 2017 at 02:50 PM
Never mind apparently it has to do with the cursor lock the fix is to set the cursor lock to none and then the player can enable cursor like himself.