Just Starting Out. Trying to understand Scripting

Hello everyone, first post here.

First off i have gone through quite a few tutorials, i feel i have the basics of unity down fairly well. Im currently working on a project where we have an object with many doors, switches, buttons and so on that all need to be interacted with. The model is done, all animation is set in 3dsmax. I have been able to import it into Unity through FBX, thats working fine and the animation has followed.

I would just like to start with something very basic. So i can wrap my head around scripting. Its something that has always been hard for me to understand :stuck_out_tongue: My goal for right now is just to be able to bind one of the animations of this object to a mouse click. So every time i click the left mouse button, a specific animation will play.

Searching the forums and web many times i can find similar topics but cant seem to pull what i need out of them to get the basic understanding of how i need to apply the animation, call it and where to direct the scrip to make it all come together. If i have missed a perfect tutorial for this please let me know but i have noticed most everything involving animation is either for a character, or involving triggers that act on the location of the character. If you have any questions or need clarification please reply!

Thanks for any help you can provide :slight_smile:

Edit:

So with some more digging i found this little helpful piece of treasure.

function Update () { 
if (Input.GetButtonDown ("Fire1")) { 
animation.Play("drawoneopen"); 
} 
}

Which once i insert my animation name, and attach it to the objects root that has the animation. It works! So exciting. This is one step down a long road haha.

Odd things that are happening:
Its incredibly choppy when it plays the animation, maybe its just my import or something? It seems to of started getting like this after i went into the object and defined the first few animations and added a collider to it from there. Once the animation completes playing, its very smooth again.

Edit2: well the choppy animation i think was due to the creation of the collider when importing the mesh and adding animation, its a very complex vehicle. I assume adding a collider to it probably is choking when animating the 1.5 million polys? Unchecked that and its smooth as butter now.

Next step i need to take, it somehow assign something to a door so that when i click on it, it plays. Ill keep lookin, thanks if you end up chiming in here with a tidbit :smile:

Edit2:Been looking around and i see these OnMouseDown functions that look like they could work? Would i be able to add box colliders to each individal mesh inside this object that needs to be clicked? Is that a good way of doing this? In the end there will be over 1000 parts of it that need to be interacted with in specific orders with rules of when you can do what of course. Just thinking about the future.

If there are any other colliders on the object and they are intersecting, the physics system will get unhappy, which would cause a lot of chopping there.
If thats the case and there are colliders intersecting, one way to remedy that is to ignore collisions between the two colliders, if they aren’t required that is. You would do it with the Physics.IgnoreCollision() function.
Also be wary of your draw calls. You said that the mesh is 1.5 million triangles. If that is all one object, it should only generate a couple of draw calls, whereas if it is divided up into a lot of smaller objects, it will generate a lot of draw calls, and slow down your rendering a lot. To check your current draw call count, press the stats button in the top right of your scene view. It should give you a lot of info about what is being computed each frame.

What do you mean when you say choppy? Is the actual animation jerking, or is it an abrupt jump from one animation to the next? If it’s the latter case, have you looked at using CrossFade() or Blend() in the Animation component?

Yes well, you probably don’t want to use mesh colliders made out of 1.5 million polys. Most of the time, you can get a away with approximating it with a bunch of primitives, which in general are extremely fast compared to mesh colliders.

By comparison, you can probably have a few hundred primitive colliders to approximate your mesh, and it will still be lightning fast.

Thanks for the replies!

So i checked the draw calls. Im currently at 876 for the exterior of this vehicle. Inside of each panel that i need to open, there is a ton more stuff. I just checked in max and the total vehicle will be 5800 objects. Right now how i have it divided up, is just the exterior with the entire thing closed up for the first import, then im thinking ill try to either import each doors interior separately or, all at the same time depending on how unity handles it. And i misspoke earlier, its at 130k triangles. The total mesh including the interior will be at around 1.5million tris.

As for the choppy animation, it was as if the frame rate dropped to 3fps, the camera was jerking as well not just the animation. What i meant was during the actual playing of the animation the whole scene became choppy. Hope that helps. Also im sure there are many points of intersecting geometry that might of made it hard for the collider when using its own mesh. I think ill just have to go with the box method which im fine with. Would this be a good way to set up hitbox type things for the clicking of each individual door and switch i will need to interact with?

What im trying to figure out right now is what is the best way to go about interacting with this object. Right now i only have the exterior of this vehicle. There are at least 12 doors i need to open. I have tried adding a collider and onmousedown script to smaller child objects of the main root part of the vehicle, but that does not seem to work at all. Would a good path for this be something similar to this? (i cant write java well at all so here is the gist of it)

function onmousedown()

get which collider is being clicked (can you click through colliders and accidently click 2?)

animation.play(whichever collider is being clicked)

Im going to mess with this for a while today, any insight would be greatly appreciated! thanks for the help.

Edit:

I found this piece of code, looks like it might be what im working towards? Its hard for me to interperate it and impliment it to my object though but if im on the right track lemmi know!

private var Children; 

function Start() { 
   // this call the function "AssignChildren()" inside this script 
   AssignChildren(); 
} 

// create an array an assign the children 
function AssignChildren() { 
   Children = new GameObject[transform.childCount]; 
   var i = 0; 
   var childs = GetComponentInChildren (Transform); 
   for (var ch : Transform in childs) { 
      Children[i] = ch.gameObject; 
       i++; 
   } 
    
   // this calls the function "PlayAnimationInChildren" with the name of the animation 
   // use this wherever you want inside this script 
   PlayAnimationInChildren("grow"); 
} 




// this function starts all animations 
function PlayAnimationInChildren(animName : String) { 
   for(var i=0; i<Children.length; i++){ 
      Children[i].animation.Play(animName); 
      // code here happens to all children 
      // eg. "Destroy (Children[i]);" will destroy all the children 
   } 
} 

// same as above, but this stops all animations, just use "StopAnimations();" 
function StopAnimations() { 
   for(var i=0; i<Children.length; i++){ 
      Children[i].animation.Stop(); 
   } 
}

Here is the thread its from,

http://forum.unity3d.com/viewtopic.php?t=25100&highlight=play+child+animation