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
2
Question by TLoch14 · Mar 07, 2012 at 11:46 PM · animationcontrollercontrolbestpractices

Script Management best practiecs

Hello people who are smarter than me.

I'm am just getting into some of the more subtle techniques of programming and had a question about script management in Unity.

From my studies and talks with more experienced programmers than myself, I've come across the concept of control a lot. What script is in control of this one? Who is setting this object's values? Who is getting them? Are you sending messages up and not down? What script owns this script?

Some of it is well above my head, but I am running into a problem that I'm not sure the best way to tackle it.

It is basically this: I have two scripts on my character prefab. He has a couple animations--run, idle, and two attacks. All of the animation tutorials I have looked at seem to have the animation script and the controller script separate things. This I don't have a problem with, necessarily. What bothers me is that the controller script will do something like this:

 //This is the controller script
 function Update() {
     if (Input.GetKeyDown(KeyCode.W) {
         //Make the character run forward.
     }
 }

And then in the animation script, they would do something like this:

 //This is the animation script
 function Update() {
     if (Input.GetKeyDown(KeyCode.W) {
         //Crossfade in the run animation.
     }
 }

My issue is that it feels like you build inherit problems of your animations not necessarily syncing up with your actions. I can understand that you want a separate script for setting up any animations, layers, wrap modes, and animation events--that much makes sense. But to have the two both looking for the same action seems like you're creating more work for yourself in the long run and not really understanding who is in control of what.

Which brings me to my question of "What is the best practice here?"

I was thinking something like this:

 //This is the controller script
 var anims : AnimationScript;
 anims = GetComponent("AnimationScript");
 
 function Update() {
     if (Input.GetKeyDown(KeyCode.W) {
         //Make the character move forward.
         //Crossfade the animation via the animationscript and a
         //custom function to handle weight blending.
         anims.StartRunning();
     }
 }

Thank you in advance for your wisdom on this matter.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by apocriva · Apr 10, 2012 at 04:52 PM

In my experience, the way to approach this kind of thing is to have the user input stuff behave independently from other systems. I wouldn't have the user input code directly in my character controller. Rather, I'd have some kind of "user input" script that I could theoretically attach to any game object.

In the project I'm working on right now, for example, I have a component which handles all actor movement, whether driven by AI or by user input. It has methods like MoveForward, TurnLeft, etc. This works for me because of the block-aligned nature of movement, but I could easily change things to work in a more fluid environment by having the movement script use impulse flags instead of one-shot movements.

 if(Input.GetKeyDown(KeyCode.W)) {
     GetComponent<CharacterController>().ForwardImpulse();
 }

^ this would be in the user input script, and then in the update of the character controller...

 if(hasForwardImpulse) {
     // If we are in an idle state at the moment,
     // kick off the animation and whatnot.
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Animation playing when NOT grounded 0 Answers

Controlling animation with mouse 0 Answers

How to customize an Animator Controller? 0 Answers

UCE0001: ';' expected. Insert a semicolon at the end 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