- Home /
Getting the rotation of an object from another object within a prefab.
I'm not really even sure how to go about asking this, but here goes. My overall goal is to make a character's eyes move by adjusting the mainTextureOffset by using the rotation of some bones in the character's eyes as the x and y values.
So I am trying to assign a transform variable on a script that I have attached to a character's eyes. Problem is that the character is created from a prefab on start, so I can't simply drop the object in the transform slot. I need to assign the transform through scripting. The eye bone is in the same object as the eye with the texture, but down a different branch of the hierarchy. So how would I access the rotation transform for the eye bone?
Here is my script so far. It works as I need it to with the exception of assigning that transform variable.
var eyeBone : Transform;
function Start ()
{
eyeBone = //Not sure what I need here.
}
function Update ()
{
var eyeBoneAngleX = eyeBone.transform.localEulerAngles.x;
var eyeBoneAngleY = eyeBone.transform.localEulerAngles.y;
renderer.material.mainTextureOffset = Vector2(eyeBoneAngleX,eyeBoneAngleY);
}
quick question: do you use
GameObject characterFromPrefab = Instantiate(prefab, pos, rot) as GameObject
or just Instantiate? :)
I use:
for (var i = 0; i < numberOfCharactersToCreate; i++)
{
characters[i] = Instantiate(Resources.Load("Prefabs/prefab_character")) as GameObject;
}
Answer by SBull · Sep 18, 2015 at 05:59 PM
I have been digging through forums and manuals for the past few hours and finally figured it out. Runalotski, you were very close with
eyeBone = transform.Find("Spine1/Spine2/Neck/Head/Eye).transform;
I tried this early on, but it still wasn't working right, because it needs to start from the root. So I finally learned I needed to add in "root" to let it know that I wanted it to start searching from the top of this object's hierarchy.
eyeBone = transform.root.Find("Spine1/Spine2/Neck/Head/Eye").transform;
This finally gave me the result I was looking for.
Answer by Runalotski · Sep 18, 2015 at 05:39 PM
First try making an instance of the object in your scene and then just drag the child object "Eyes" into the public transfrom then at the top of the Inspector there is an apply button click that and this reference will be saved in the prefab.
or
on line five where you wrote
eyeBone = //Not sure what I need here.
you can use something similar to
eyeBone = transform.Find("eye").transform
now i dont know what the path is to your eye bone but you may need to path from the object with your script to it like such
eyeBone = transfrom.Find("Spine1/Spine2/Neck/Head/Eye").transfrom
you could also make a tag called eye and tag the bone with it then you can use
eyeBone = transform.FindObjectWithTag("eye");
Answer by vashtriguner · Sep 19, 2015 at 03:30 AM
You could just name the child object something like "Eyes" or something then do a transform.root.FindChild("Eyes");