- Home /
instantiating transform.translate in the wrong position?
This is likely a silly mistake I'm making but it has me stumped.. I have an object in this example which takes customization for ease of explaining we will say its a car. The "car" is loaded into the scene with an asset bundle the loader script is attached to a null object targeted by a camera. This "car" loads in and is placed in the scene with a rotation and translation. All this works fine.
The trouble is this "car" has objects that it loads from assetBundles, accessories, And they load in the wrong place. For now lets just say the tires are loaded separate from the car allowing the tire to be say a sports tire or a monster wheel. So once the car is in place it has a script which loads more assets. It loads a single tire and instantiates the tire 4 times. but the tires are not going where I tell them to. The Y position and the rotation are right but the X and Z are wrong.
So here is what my code looks like.
yield return download;
assetBundle = download.assetBundle;
loadedAsset1 = Instantiate(assetBundle.mainAsset) as GameObject;
loadedAsset1.name = Asset_One;
loadedAsset1.transform.Rotate(R1, R2, R3);
loadedAsset1.transform.Translate (targetOnePosition.x,targetOnePosition.y,targetOnePosition.z);
loadedAsset2 = Instantiate(assetBundle.mainAsset) as GameObject;
loadedAsset2.name = Asset_two;
loadedAsset2.transform.Rotate(R1, R2, R3);
loadedAsset2.transform.Translate (targetTwoPosition.x,targetTwoPosition.y,targetTwoPosition.z);
Rinse and repeat for the other two "tires"
As you may be able to guess from the code I have public variables allowing me to set the name, translation and scale for each instantiated object. I could use a vector3 for targetRotation the same way I do for targetOnePosition, or use floats of P1,P2 and P3 for the positioning as I have for the rotation in the above example. Either way the results are the same. The rotation currently works fine (I'm only rotating the "y" axis anyways) but the transform only gets the right float for the Y. if I set it to (0,1,0)its OK but (1,1,0) turns into (-0.7071069, 1, 0.7071067)
Once I can get the "tires" on I will need to be able to add a second, third, fourth or more assets for accessories like spoilers or... I don't know hood spikes. I will deal with that later but for now does anyone know why the instantiated copies of an assetBundle would reposition themselves?
the positioning code on the base "car" frame looks like this:
yield return download;
assetBundle = download.assetBundle;
loadedAsset = Instantiate(assetBundle.mainAsset) as GameObject;
loadedAsset.transform.Rotate(0, 45, 0);
loadedAsset.transform.Translate(0, 1, 0);
It comes in right where I want it, however if I change the transform.translate from (0,1,0) to something like (2,1,0) the object is instantiated at (1.41423, 1, -1.414214) instead? So why is the translate.Transform not moving objects where I'm telling it to?
The assetBundles Base position and rotation is zeroed out when the bundle is made so i don't think I have any competing transforms or rotations as seen in similar posts.
Any suggestions would be appreciated Thanks.
Answer by Giantbean · Jan 23, 2014 at 10:18 PM
I swapped:
loadedAsset.transform.Translate (targetOnePosition.x, targetOnePosition.y, targetOnePosition.z);
for:
loadedAsset1.transform.position = new Vector3(targetOnePosition.x, targetOnePosition.y, targetOnePosition.z);
And it is working now. Seems to be something with the local vs object position and rotation.
I read the documentation on both before making my first post but it is still confusing to me. It seems translate takes local rotation of the object into account and position does not. Using transform i would have to convert the local to world space but with position it is moving in world space without any extra code.
I'm not sure which way is better but my code is working now so I'm OK with it.
If anyone wants to correct me on any this feel free.
Your answer
Follow this Question
Related Questions
Auto Translate after cloning gameobject using Instantiate() 1 Answer
Create an AssetBundle with prefabs containing imported models 0 Answers
Custom movement problem 1 Answer
How to keep cameras Vector3.y Position While Translating it. 1 Answer
Position an instantiated prefab relative to its translation vector 2 Answers