Hello, i’m more or less a beginner at Unity and C#, my code might be terrible and my techniques might be stupid, but i already know that, all i am here for is to see if someone has time to give a noobie a hand, i’ve been trying to figure this out for a while but nothing comes to mind.
I commented the lines to make the code slightly less confusing, hope you find it understandable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class PlaceCable : MonoBehaviour {
Camera cam;
GameObject[] childrenOfObj;
GameObject[] anchors;
public GameObject itemInHand;
int childCount;
int anchorCount = 0;
void Start () {
cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
}
void Update ()
{
Place();
}
void Place()
{
if (Input.GetMouseButtonDown(1))
{
int anchorC = 0;
Ray ray = cam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0.0f));
RaycastHit hit;
if (Physics.Raycast(ray, out hit) && !hit.collider.gameObject.CompareTag("Player") && !hit.collider.gameObject.CompareTag("MainCamera")) //if did not collide with player
{
childCount = hit.collider.gameObject.transform.childCount; //how many children does the hit object have?
childrenOfObj = new GameObject[childCount]; //add those children to an array
for (int a = 0; a < childCount; a++)
{
childrenOfObj[a] = hit.collider.gameObject.transform.GetChild(a).gameObject;
if (childrenOfObj[a].gameObject.CompareTag("AnchorPoint"))
{
anchorC++;
}
} //for every anchor in those children, increase the anchor count by 1.
anchors = new GameObject[anchorC]; //initialize the anchors array with lengt of found anchors (anchor count).
for (int a = 0; a < childrenOfObj.Length; a++)
{
if (childrenOfObj[a].gameObject.CompareTag("AnchorPoint"))
{
anchors[anchorCount] = childrenOfObj[a];
anchorCount++;
}
} //for every child the array of children that is an anchor, add it to the array.
print("Gameobject " + hit.collider.gameObject + " has " + childrenOfObj.Length + " children" + " and has " + anchors.Length + " anchor(s)");
for (int a = 0; a < anchors.Length; a++)
{
print(anchors[a]);
}
GameObject itemToPlace = Instantiate(itemInHand);
if (itemToPlace != null)
{
int itemChCount = itemToPlace.gameObject.transform.childCount;
int anchCount = 0;
GameObject[] itemCh = new GameObject[itemChCount];
GameObject[] itemAnchors;
if (itemChCount > 0) //if the item has children
{
for (int a = 0; a < itemChCount; a++)
{
itemCh[a] = itemToPlace.gameObject.transform.GetChild(a).gameObject; //add those children to an array
}
for (int a = 0; a < itemChCount; a++)
{
if (itemCh[a].gameObject.CompareTag("AnchorPoint"))
{
anchCount++; //for every anchor in that array add 1 to the count
}
}
itemAnchors = new GameObject[anchCount]; //initialize array with lenght of how many anchors there are
float[] distances = new float[anchCount]; //array of floats with lenght of how many anchors there are
for (int a = 0; a < anchCount; a++)
{
if (itemCh[a].gameObject.CompareTag("AnchorPoint"))
{
itemAnchors[a] = itemCh[a]; //for every anchor in the array, add it to anchor array
}
}
for (int a = 0; a < anchCount; a++)
{
distances[a] = Vector3.Distance(hit.point, itemAnchors[a].transform.position); //for every anchor determine how far it is from the Raycast hit point and add it to the array
}
float smallestDistance;
smallestDistance = Mathf.Min(distances); //calculate the smallest distance in the array
GameObject ancWithSmallestDistance;
for (int a = 0; a < anchCount; a++)
{
if ((Vector3.Distance(hit.point, itemAnchors[a].transform.position) == smallestDistance))
{
ancWithSmallestDistance = itemAnchors[a]; //find out wich anchor in the array has the smallest distance
itemToPlace.transform.position = new Vector3(ancWithSmallestDistance.transform.position.x, ancWithSmallestDistance.transform.position.y, ancWithSmallestDistance.transform.position.z); //place the item on the anchor.
print(ancWithSmallestDistance + " is the closest anchor wich is part of " + hit.collider.gameObject);
print(itemToPlace + " is the item to place");
}
}
//print("The smallest distance is " + smallestDistance);
ancWithSmallestDistance = null;
anchCount = 0;
smallestDistance = 0;
}
}
anchors = null;
childrenOfObj = null;
childCount = 0;
anchorCount = 0;
}
}
}
}
Now i will explain a bit more in depth my issue and where it’s happening.
I am making a game regarding machines, electricity and all that good stuff. These machines need power, and there already is a generator that takes energy from mined coal and stores it in an internal storage. What i’m trying to do here is to be able to place some cables on an object and the cables must snap to that object’s “Anchor” wich is an empty GameObject with the tag “AnchorPoint”.
So, player has a cable in his hand, presses right mouse button, and the cable get placed on the object he is looking at if the object has an anchor point.
The box colliders you see on the sides aren’t really of use, they’re just there as a reference, what matters is the empty gameobjects that they appertain to.
I don’t expect you to understand my code or to find out the issue, but maybe someone is willing to give it a shot, there is no harm in trying.