Making a child take the parents position and rotation.

I thought I’d make a video to illustrate what I am trying to do as I can’t seem to find the words to make it sound right for others to read!

I am trying to put the red peg in a “slot” in black cube (the slot is where the white cube is placed). The white cube is a child of the black cube and I need the red peg to be a child of the white cube. I am only really accessing the white cubes position and rotation as far as I know; but this is where my trouble lies. I can put the peg in the right position but the rotation is not right. The peg needs to stick out of the slot in the way the initial peg that I am dragging looks like, not how it looks when a new peg is instantiated.

This is my current script which is controlling this action:

using UnityEngine;
using System.Collections;

public class Slot : MonoBehaviour {
	public bool hasPegAttached;
	public Vector3 slotPosition;
	public float zPos;
	public float zAdjust;
	public float xPos;
	public float xAdjust;
	public bool overSlot = false;
	public bool hit = false;
	public GameObject pegObject;
	public GameObject slotObject;
	public Transform cubeObj;
	

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		attachToSlot();
	}
	
	void OnMouseOver()
	{
			hit = true;
			overSlot = true;
	}
	
	void OnMouseExit()
	{
			hit = false;
			overSlot = false;
	}
	
	public void attachToSlot()
	{
		if(Input.GetKeyUp("space"))
		{
			if(overSlot == true  hit == true)
			{
				GameObject peg = Instantiate(pegObject,transform.position,Quaternion.identity) as GameObject;
				
				peg.transform.parent = transform;
				peg.transform.rotation = transform.rotation;
			}
		}
	}
}

How would I go about making it so that the peg takes the correct rotation of the slot? The cube is freely moveable and can be stopped in any rotational point. This is what is causing the trouble; we had it working before with the peg snapping properly but now this has messed it up!

Thanks

Cas

This bit doesn’t strike me as too likely:

				peg.transform.parent = transform;
				peg.transform.rotation = transform.rotation;

If the peg is going to be a child of transform, then you don’t need to also copy the rotation — that would apply the rotation twice, as it were. So, either don’t set the peg.transform.parent, and just copy the position and rotation; or do set the parent, and don’t copy either one (especially rotation).

HTH,

  • Joe

Maybe there are offsets you need to check. If you manually set the red peg as child of the white cube and then zero out the position and rotation in the inspector, do they line up properly? If so, then the code example should work.

Otherwise you might need to realign your model appropriately in your modeling program, or do an offset like
peg.transform.rotation = transform.rotation + Quaternion.Euler(0,180,0); //(i think)

P.S., you could also just do:
GameObject peg = Instantiate(pegObject, transform.position, transform.rotation) as GameObject;
instead of adding the
peg.transform.rotation = transform.rotation;

no - just setting a transform as a child of another does not copy the rotation or position values. You need to do both when initially setting up the hierarchy. Otherwise you could make one object that is 100 units away the child of another, and it would still be 100 units away, and if you rotated the parent, the child would rotate in a circle 100 units in radius.

Once the hierarchy is in place, then you would not necessarily have to update the rotation of the the child.

However, he would not have to do
peg.transform.rotation = transform.rotation;
if he did
GameObject peg = Instantiate(pegObject, transform.position, transform.rotation) as GameObject;
peg.transform.parent = transform;

Cheers for the replies. I tried adding the “+ Quaternion.Euler(0,180,0);” but I had an error saying; “Operator +' cannot be applied to operands of type UnityEngine.Quaternion’ and `UnityEngine.Quaternion’” and for some reason if I just put -

peg.transform.rotation + Quaternion.Euler(0,180,0);

it flattens the peg >.<*!

I also tried just using GameObject peg = Instantiate(pegObject, transform.position, transform.rotation) as GameObject; but that didn’t attach the peg as a child.

I’m properly confused atm. Just can’t get my head around it!

no, that only matches the position and rotation all at once inside the Instantiate function. You would still need to set the parent in the next line. (peg.transform.parent = transform; )

Good thing - you just need to match the rotation of the peg with the white cube but with an offset, because they are not oriented in the same way (you could also just pre-rotate your white cube so that the the peg will match properly!)

Ok, I rotated the white cube to make it work and…it did, thanks ^_^.

Now how can I make it so the game object (peg) I am dragging will fit in the “slot”, giving the snapping effect instead of me having to instantiate a new peg and destroy the old one? I see no sense in destroying a perfectly good game object…

Any ideas?

Cheers

Sure…
How about:

The user has control over the position of the peg and rotation of the cube
then the user clicks to see if the peg will go into the slot
if it will, then do something like this:

I see no sense in not referring to perfectly good code example… :slight_smile:

oh wait - you still want the snapping effect…
Then just set the peg to the white cube position and rotation immediately after removing it from the control of the user.

I can’t use those I’m afraid. The person I am making it for would like the player to constantly have to have their finger on the object (as this will be a mobile game but we are testing on PC until we get a full license) to drag it and a halo will show if it can go in the slot.

We’re using those bits of code to make the peg slowly go back to its position if the player lets go of the peg before it is/can be placed. This is just a test scene and it doesn’t have the mouse drag script we have in our main scene. When a texture is clicked in the main scene the peg will instantiate and auto attach to the mouse and follow it until the mouse button is clicked.

What we would ideally like is that when the mouse button is clicked and it can go in an available slot, that peg will snap in to the position instead of making a new instance of it and destroying that one.

right - I remember our earlier posts about the returning to its original position.

so skip the lerping stuff -

what about my last sentence? Just stop letting the user drag the peg and set the peg to the white cube position/rotation and do not destroy it?