- Home /
The script isn't taking in the right transform positions!
I have a script where once two objects get close enough together it calls a function that only prints out something at the moment ,but will do something else later. The objects that I am checking the distance of are parented under a simple shape of some sorts and I have the Transforms in the function set to the children of the object yet the function only responds to the distance from the parent and ignores the children for some reason. I do not understand why this is happening. Here is the script. I am checking the distance and assigning the transforms in the SnapTry function and the SnapTry fucntion calls the Snap function. Thank you for any help at all.
using UnityEngine;
using System.Collections;
public class PlacingParts : MonoBehaviour
{
private Transform currentPart;
public Transform centerPoint;
private int partLayerMask = 1 << 9;
private int snapPointLayer = 1 << 8;
private bool hasPlaced;
public float snapPointDistance = 0.2f;
void Start ()
{
hasPlaced = false;
}
void Update ()
{
//HOW TO PLACE THE ITEM
if (currentPart != null && hasPlaced == false)
{
//GIVES ME THE ABILITY TO MOVE THE PART/OBJECT THAT WAS JUST INSTANTIATED WITH THE PLACEITEM FUNCTION
Vector3 m = Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 12.9f +( MouseOrbitImproved.distance - 13.5f)));
currentPart.position = new Vector3(m.x,m.y,m.z);
//SET THE ITEM TO HAS PLACED SO THAT IT STOPS MOVING
if(Input.GetMouseButtonDown (0))
{
hasPlaced = true;
}
}
//HOW TO PICK THE OBJECT BACK UP AFTER IT HAS BEEN PLACED
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, Mathf.Infinity, partLayerMask))
{
}
}
void SnapTry(Transform obj1, Transform obj2) {
Transform[] transforms1 = obj1.GetComponentsInChildren<Transform>();
Transform[] transforms2 = obj2.GetComponentsInChildren<Transform>();
foreach(Transform tr1 in transforms1) {
foreach(Transform tr2 in transforms2) {
if (tr1.tag == "SnapPoint" && tr2.tag == "SnapPoint") {
if ((tr1.position - tr2.position).sqrMagnitude <= snapPointDistance) {
Snap();
}
}
}
}
}
//SNAPS THE TWO POINTS(THE ONE YOU HAVE SELECTED AND THE ONE THE RAY HITS)
void Snap()
{
Debug.Log ("You could snap the object now");
}
public void PlaceItem(GameObject b)
{
hasPlaced = false;
currentPart = (((GameObject)Instantiate(b)).transform);
}
}
Answer by robertbu · May 08, 2014 at 04:34 AM
Nothing in this script is calling SnapTry().
Somewhere (in Update() probably) you need to do:
SnapTry(currentPart, centerPart);
Oh, that was simple ,but there is a lot more I need to do it seems. I have to make SnapTry take an array of already placed parts/gameobjects transforms for obj2 so that the snap script can work for every object with snap points. But thank you, I feel dumb now.
I know in your other question we discussed how to parent the object when combining them. There is one other issue that only occurs to me as I answer this one. When you parent the objects to an empty game object, I believe you want to change the tag. So your snap points have one tag, but the visible object should also have a (different) tag. When you combine them, you would remove the tag from both objects and put the tag on the empty game object parent. That way you can do a GameObject.FindObjectsWithTag() and find all the top level (combined) objects as well as any single objects. Finding top level is necessary so that SnapTry() will find all the transforms associated with combined object.
Yeah, I understand. It's a sometimes fun to have a challenge like this scrip here.