- Home /
Other
Placing an object on the side of another object?
Hello! I am working on a placing system for a game i am creating
The objects are going to be different shapes and sizes from one another, And these objects are going to 3D I'm trying to set the position of the selector on the side of another object And then i am trying to round the position for more a a grid-like placement
So far i have this code:
public static bool editMode = true;
public GameObject test;
static Material selectorG;
static Material selectorR;
public static GameObject selector;
public static bool canPlace;
public static bool snapping = true;
//This function is being called in LateUpdate()
if (Physics.Raycast (ray, out hit)) {
Vector3 hitSide = hit.normal;
selector.gameObject.SetActive (true);
Mesh model = go.GetComponent<MeshFilter> ().sharedMesh;
selector.GetComponent<MeshFilter> ().mesh = model;
Vector3 newPos;
// Setting the position of the selector
// If snapping is disabled
if (snapping == false) {
//This works
newPos = new Vector3 (hit.point.x + Mathf.Ceil (hitSide.x) / 2, hit.point.y + 0.001f + Mathf.Ceil (hitSide.y) / 2, hit.point.z + Mathf.Ceil (hitSide.z) / 2);
// If snapping is enabled
} else {
//This doesn't work.. Instead it floats off the ground, And if you put your mouse against the wall, It will float off the wall too
newPos = new Vector3 (Mathf.Round(hit.point.x + Mathf.Ceil (hitSide.x) / 2), Mathf.Round(hit.point.y + 0.001f + Mathf.Ceil (hitSide.y) / 2), Mathf.Round(hit.point.z + Mathf.Ceil (hitSide.z) / 2));
// And the other way of doing this..
//This doesn't work because it intersects the object it hits, And i want it to be rounded..
newPos = ray.origin + (ray.direction * hit.distance);
}
selector.transform.position = newPos;
selector.transform.localScale = localS;
//Setting the material accordingly
if (canPlace) {
selector.GetComponent<Renderer> ().material = selectorG;
} else {
selector.GetComponent<Renderer> ().material = selectorR;
}
//Rotation.. Ignore this
if (Input.GetKeyDown ("q")) {
selector.transform.Rotate (Vector3.up * 90);
}
if (Input.GetKeyDown ("e")) {
selector.transform.Rotate (Vector3.up * -90);
}
// Placing the object when you click the mouse
if (Input.GetMouseButtonDown (0) && canPlace) {
GameObject newObj = Instantiate (go);
newObj.transform.position = selector.transform.position;
newObj.transform.rotation = selector.transform.rotation;
}
} else {
//Making the selector disappear when there is nothing the mouse is hitting nothing
selector.gameObject.SetActive (false);
}
}
And from line 48 to 59 i am setting the position
Vector3 hitSide = hit.normal;
if (snapping == false) {
//This works
newPos = new Vector3 (hit.point.x + Mathf.Ceil (hitSide.x) / 2, hit.point.y + 0.001f + Mathf.Ceil (hitSide.y) / 2, hit.point.z + Mathf.Ceil (hitSide.z) / 2);
// If snapping is enabled
} else {
//This doesn't work.. Instead it floats off the ground, And if you put your mouse against the wall, It will float off the wall too
newPos = new Vector3 (Mathf.Round(hit.point.x + Mathf.Ceil (hitSide.x) / 2), Mathf.Round(hit.point.y + 0.001f + Mathf.Ceil (hitSide.y) / 2), Mathf.Round(hit.point.z + Mathf.Ceil (hitSide.z) / 2));
// And the other way of doing this..
//This doesn't work because it intersects the object it hits, And i want it to be rounded..
newPos = ray.origin + (ray.direction * hit.distance);
}
All help is appreciated :)
Good day @creepercraft187 !
I try to help a lot of peopple, in order to help me to help you, tell me whats exactly / what lines you have problems? What do you need?
Is your code working fine? At line 47 :
if (Input.Get$$anonymous$$ouseButtonDown (0) && canPlace) {
GameObject newObj = Instantiate (go);
newObj.transform.position = selector.transform.position;
newObj.transform.rotation = selector.transform.rotation;
I recommend you do to it in a single line, so the object instantiate directly at the corect position
GameObject newObj = Instantiate (go, selector.transform.position, selector.transform.rotation);
But is just a little thing... Tell me where is the problem.
Bye :D
Answer by tormentoarmagedoom · Sep 19, 2017 at 07:41 AM
Hello @creepercraft187 !
Whats the problem ? I didnt read the code (so long and i dont kno what you need). Whats the question? I can give you "general examples" of code for something, but i dont know what you want exactly.
Ask/Give info using @tormentoarmagedoom