I am trying to create a transform in code, inside the update function by going
var newTrans = new Transform;
But that just doesn’t work.
and if I go
var newTrans : Transform;
it makes an uninitialized transform that cannot be used
and going
var newTrans: Transform;
newTrans = new Transform;
doesn’t work either
How do I create a new, usable, empty, transform inside of the update function?
The reason I need to do this is because I need to use the translate function to move an object a specific direction, but that object itself cannot be rotated. So I am trying to initialize a second transform, aim that second transform the direction the object needs to translate, then translate the second transform, while setting the original object transform equal to the position of this new transform.
Is there a better way to accomplish the same thing?
This was one of my first questions as well. Coming from other 3D applications, especially Softimage|XSI, getting an ‘empty’ transform, then filling it in before setting it to an object is a nice work-flow.
You can’t do that here though. The most granular objects you can work with are rotation (quaternions) and position (Vector3). This is all much more clear in C#.
If you are used to working on objects as your question suggests, you might want to try C# if you have the flexibility. I knew absolutely nothing about C# (or C or C++) until about 3 weeks ago. Now I am pretty much fully productive. My only previous experience was JavaScript and Python - and some older scripting languages not worth mentioning.
You can’t have a transform without a game object; Transform is a component that’s attached to a GameObject. All GameObjects have a Transform, which is the only component that’s not optional. So you can do “var newTrans = new GameObject().transform;”
I workaround of this is just save the position, rotation and scale separately so that i don’t need to create a hollow GameObject to store the transform
GameObject emptyGO = new GameObject();
Transform newTransform = empt.transform;
to get a transform will lead to memory leaks due to un-destroyed GameObjects. As Benson said, save the rotation/position/etc as Vector3s and Quaternions
I just had this problem. In my VR game I want to translate/rotate a transform to a destination that the player will be teleported to (using SmoothDamps). I use a handy RotateAround call that only exists in Transform., Rather than rewrite that, I put an empty GameObject in the editor, called it ‘TeleportDest’, then I modify that in my code as needed, using it each time.