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 Macro · Jul 12, 2014 at 08:43 AM · animationmovementinputmecanimbest practices

Handling input, movement, animation with scripts?

This is quite an open question however I think there is value to the answers to it.

So lets take a simple and common scenario, we have a player character and some NPCs all of them share the same animations, npcs move via pathfinding and the player moves via input.

Now ideally it makes sense to somehow share the animation controller and logic (i.e setting mecanim vars), the movement logic will differ so ideally the animation needs to be driven by the movement logic and the player also needs to be aware of input (be it raw keys or absracted input frameworks).

So the simplest and least flexible way to do it is to give 1 script to npcs and 1 to players which would do something like:

 if(Input.GetButtonDown("W"))
 {
     transform.position += transform.forward * MoveSpeed * Time.deltaTime;
     animation.Play( "walk_cycle" );
 }

However then there is no shared logic and if you need to change the animation names or keys etc it is a difficult job.

So I was thinking that it would be better to have some sort of eventing system so basically there would be a script to handle animation based upon movement which would be notified, so when an input happens it would notify the objects listening then when the player processes that and lets say moves forward then it raises an event of some sort for a shared animation script to listen to on each enity.

Anyway there are lots of ways to solve this problem but I wanted to see what everyone else would say is best practice on this as I find it very hard to find the unity best practices.

Comment
Add comment · Show 3
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 infinitypbr · Jul 12, 2014 at 08:48 AM 0
Share

This is a better topic for the forums, since it's not really a "Question" but a "lets discuss ideas".

However, I would suggest making one animator for each main thing. If all NPCs are alike, then one for them is fine, but I would shy away from making some sort of universal animator. I tried that in my game, and it's not as good as it sounds, it actually is causing me problems. It's not hard to copy a current one and make simple modifications to make it unique.

avatar image TonyLi · Jul 12, 2014 at 12:20 PM 0
Share

Agreed; this is better placed in the forums. When you create a thread there, post the link here so others can find it.

It's very common to use a high level animation controller that abstracts away all the $$anonymous$$ecanim details. Another layer on top of that usually controls higher-level character states and gets its input from a "virtual controller". The player sets the virtual controller's values from physical input hardware. NPCs set the virtual controller values from an AI layer.

avatar image Macro · Jul 12, 2014 at 05:38 PM 0
Share

I was originally thinking about putting it in the forums but whenever I search for these sort of topics I always find the best answers in the unity answers sections so put it in here, but like you say there is no SINGLE right answer, so will put this post in the forums and leave a link to this.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JusticeAShearing · Jul 12, 2014 at 04:01 PM

Here's my java-script for movement. I think that it suits your purposes. It also holds animation code, gravity, rotation and jumping.

 var speed : float = 6.0;
 var twiceSpeed : float = 10.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 var rotateSpeed : float = 3.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Update() {
     var controller : CharacterController = GetComponent(CharacterController);
     if (controller.isGrounded) {
         //Grounded, so recalculate
         //Move directly from axes
         moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
         
         //Rotation Code
         transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
         
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         
         if (Input.GetButton ("Jump")) {
             moveDirection.y = jumpSpeed;
         }
         
         //Animation Code
         if (Input.GetKeyDown("w"))
         {
             animation.Play("Full Walk");
         }
         else if (Input.GetKeyUp("w"))
         {
             animation.Stop();
             animation.Play("Idle");
         }
     }
     
     //Apply Gravity
     moveDirection.y -= gravity * Time.deltaTime;
     
     //Move Controller
     controller.Move(moveDirection * Time.deltaTime);
 }
Comment
Add comment · Show 2 · 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 Macro · Jul 12, 2014 at 05:37 PM 0
Share

Yes this is the normal sort of approach but I do not think it is best practice to put it all in one script as it means your animation and movement etc is driven by specific key presses not events. What if you are in a menu or your character should not be moveable etc, if you split it out into different chunks of logic which drive each other its easier to test and refactor when needed. A valid answer though.

avatar image JusticeAShearing · Jul 13, 2014 at 04:05 PM 0
Share

Okay, tell me if you can't split it into different scripts. I think that one could also write a bit of Java-Script to make it so that all of the variables become nothing when the menu is brought up.

Besides, I have the game pausable recently, and there were no problems. Also, errors tend to specify which lines that it occurred on.

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Benefits of root motion 1 Answer

Strange Character Movement (Mecanim) 0 Answers

Can i use mecanim animator while using legacy? 1 Answer

Animate mecanim character only from script 1 Answer

setting animation transition times to joystick float values 0 Answers


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