Hi all,
Looking for some help with this:
I want to place a cube in 3d space at mouse posistion only if there isn’t a collision.
I have this working in another project but can’t get it to work.
My understanding that for a collision to detect a collider needs to be set as trigger and ridgebody
in my project
Cube prefab has box collider only.
GhostCube prefab has box collider is trigger and a rigidbody
My code using UnityEngine;using System.Collections;public class building : MonoBeh - Pastebin.com
I am moving the ghostCube around with transform.posistion
I have tested the collision between objects and it works however when user presses to create object it is allowed to be placed
found a few key issues, the main one being i didn’t remove the old ghost before placing the new cube! (that would help)
but problems remain - - this isn’t quite resolved.
the code works as intended for Input.GetMouseButtonDown but not for Input.GetMouseButton so this leads me to believe that its placing before the collision detection can kick in? i have changed the collision detection to continuous but no fix, any ideas?
if (placingMode == true)
{
//build.transform.position=hitPointGrid; //move clone with mouse
buildGhost.GetComponent<Rigidbody>().position = hitPointGrid;
/* if (Input.GetMouseButton(0)) //if left mouse button held down //single mouse press works but held down doesn't
{
if (buildGhost.GetComponent<Cube>().allowedPlace == true)
{
//do another test for buildCube allowed Place ?
DestroyObject(buildGhost); //this was what was missing!
Instantiate(cube, hitPointGrid, transform.rotation); //cast to buildCube here?
buildGhost = (GameObject)Instantiate(ghostCube, hitPointGrid, transform.rotation);
}
}*/
if(Input.GetMouseButtonDown(0)) //if left mouse button pressed
{
if(buildGhost.GetComponent<Cube>().allowedPlace == true)
{
DestroyObject(buildGhost); //this was what was missing!
Instantiate(cube, hitPointGrid, transform.rotation); //cast to buildCube here?
buildGhost = (GameObject)Instantiate(ghostCube, hitPointGrid, transform.rotation);
}
else
{
// build.GetComponent<Renderer>().material = materialRed;
Debug.Log("Not a valid area for building - collison");
}
}