- Home /
Attaching an object to a tree affected by wind?
Is it possible to attach an object to a branch node of a tree (or fake it some other way) and have it move with the wind volumes? Move but not distort. A fruit or bird, for instance, that can later be scripted to detach and be independent of the tree sway and wind.
Edit:
var tree : GameObject; tree = GameObject.Find("BigTree"); var treemesh : Mesh; treemesh = tree.GetComponent(MeshFilter).mesh; var treeVertNum; treeVertNum = treemesh.vertices.length; var dist;
function Awake (){ for (var i = 0; i < treeVertNum; i++) dist = Vector3.Distance(transform.position,tree.transform.TransformPoint(treemesh.vertices[i])); Debug.Log(dist); //if... [i]dist is less than saved dist //overwrite dist //save the new dist [i] for use later }
function Update () { //set the object to the correct position }
Answer by Flynn · Jan 19, 2011 at 10:40 PM
You could program the object to find the closest vertice in the tree's mesh, and set its position to that vertice's world location.
Oh, interesting. I didn't realize you could access a mesh's verts. This sounds difficult but very useful. How would I go about finding the closest vert of another mesh? By the way, I'm currently using javascript. Thank you for responding!
I did find $$anonymous$$esh.vertices but I'll have to look at it more later. Unfortunately, I'm out of time for now.
Iterate through all of the verts, and do Vector3.Distance(transform.position,tree.transform.TransformPoint(mesh.vertices[i])); to get the distance between your object and your vert. [tree is your tree gameobject, mesh is the mesh of your tree gameobject. And this assumes the code is on your object]
Also, you should only do the check at startup. Once you find the correct vert, save its index, and on update just set the position to tree.transform.TransformPoint(mesh.vertices[savedIndex]); Otherwise, if you have more than ten of those objects your project will seriously start to lag.
Ok. After much messing about I've cobbled together the mess I edited into my original post. That's the version that gives me the least errors at this point but I'm sure there is more than one error there. Did I mention I'm very new to this. :-) $$anonymous$$y guess is I'm not saving the mesh as a reference correctly but... Error: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) treeattach.Awake () (at Assets/scripts/treeattach.js:12)
Answer by levick · Sep 29, 2011 at 09:38 AM
It works for me, at least it prints out a position, but there is two problem: it is somehow not the closest one, and not moving with the tree.
Here it is, basically the above code with the fixes mentioned above:
ar tree : GameObject;
var closestVertIndex : int;
var treemesh : Mesh;
function Start ()
{
var treeVertNum : int;
var dist : float = 100;
treemesh = tree.GetComponent(MeshFilter).mesh;
treeVertNum = treemesh.vertices.length;
var distTemp : float;
for (var i = 0; i < treeVertNum; i++)
{
distTemp = Vector3.Distance(transform.position, tree.transform.TransformPoint(treemesh.vertices[i]));
if(dist < distTemp)
{
dist = distTemp;
closestVertIndex = i;
}
}
}
function Update ()
{
this.transform.position = tree.transform.TransformPoint(treemesh.vertices[closestVertIndex]);
print(this.transform.position);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613100207im_/https://answers.unity.com/themes/thub/images/avi.jpg)