- Home /
[solved] Get upward orientation of object and add scale value
Hey community, so i have been trying to tackle this problem which is basicly a how to question. i am trying to move an object to the edge of another object, i almost got it to work except for a 'minor' orientation problem, this is the code snippet:
currenthelditem.transform.localPosition.y = Vector3.up + snaptarget.parent.transform.localScale.y * 2;
of course the above doesn't work but it shows my intentions :) so basicly my question is how do i use the up orientation of the 'snaptarget' transform and add the localscale value to it. any ideas ?
A couple of random suggestions. $$anonymous$$ake sure that the pivot point for all the objects is where you expect, and make sure all objects are scaled (1,1,1) with consistent rotation. This can be done in a 3D authoring program, but it can also be done in Unity with some editor scripts:
$$anonymous$$oving the pivot:
http://wiki.unity3d.com/index.php?title=SetPivot
Fixing scale and rotation:
http://answers.unity3d.com/questions/561786/how-to-export-obj-from-editor-with-rescaled-mesh.html
Note the second editor script makes a clone of your object at it current scale and rotation resulting in an object with scale (1,1,1) and rotation (0,0,0).
Thanks for the suggestion, but i started out already extra carefully with neutral placeholders all evened out, in order to avoid such problems. this problem is really related to getting the size/scale of the object creating a correct offset.
Answer by Statement · Dec 23, 2013 at 04:16 PM
I have no idea what the code is trying to do but here is my best guess.
You want to position currenthelditem two units local above snaptargets parent.
var item = currenthelditem.transform;
var parent = snaptarget.parent.transform;
item.position = parent.TransformPoint(Vector3.up * 2);
Send a comment if this is not what you meant and please clarify.
Thanks for your reply! this is almost, what i'm trying to do, except, ins$$anonymous$$d of the two units i want to base this offset on the scale of the 'snaptargets parent' maybe my image helps to visualize it
So you want to rotate the chimney looking dragged object, and snap the top (well, bottom, upside down) of the chimney. The goal is to line the chimney up perfectly with the lower node of the funny onion shaped snowball?
(Is this a Christmas Game btw? :))
Lol, i haven't looked at it that way in terms of xmas, i wanted to make simple but distinct shapes to see the changes it makes before adding real models on the working code. The third image shows what happens when i snap the 'chimney' on the lower node, it should do the same as it does on the second image but upside down. it gets its rotation from the nodes, so for rotation it works, but the offset doesn't work as the image shows, what i need is to add the offset(scale of the target) based on the direction of the node.
It think this question is solved i used @statement code and changed it to:
var item = currenthelditem.transform;
var parent = snaptarget.parent.transform;
item.position = snaptarget.transform.TransformPoint(Vector3.up * snaptarget.parent.transform.localScale.y * 2);
The orientation works correctly now, except for scaling, but thats another issue.
You may want to set localScale of item to the lossyScale of snapTarget or parent. Not sure if that would solve it.
I thought for a while that the onion shape could have been Santa Claus' Bag of Gifts, but I hear what you say. It's useful to have mocks and it's a nice break from boxes, capsules and cylinders :)
Your answer
Follow this Question
Related Questions
postion scale and rotation greyed out and not working 1 Answer
Quaternion rotation with normals bug: Only works into one direction? 1 Answer
The way to approach DodgeRoll 0 Answers
why is my object's rotation affecting it's position? 1 Answer
how to CORRECTLY position and rotate a gameobject in unity 2 Answers