Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ninku · Jan 19, 2011 at 09:10 PM · windtree-creator

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 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ninku · Jan 20, 2011 at 04:33 PM 0
Share

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!

avatar image Ninku · Jan 20, 2011 at 04:53 PM 0
Share

I did find $$anonymous$$esh.vertices but I'll have to look at it more later. Unfortunately, I'm out of time for now.

avatar image Flynn · Jan 20, 2011 at 07:24 PM 0
Share

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]

avatar image Flynn · Jan 20, 2011 at 07:26 PM 0
Share

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.

avatar image Ninku · Jan 22, 2011 at 04:20 AM 0
Share

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)

Show more comments
avatar image
0

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);

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Set local acceleration on cloth? 1 Answer

How do I create accurate bend with custom tree geometry?. 1 Answer

is it possible to affect bones on skineed mesh by wind? 2 Answers

Can I stop some things in terrain > paint details from moving with the wind? 1 Answer

scale problem in tree creator 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges