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 laurienash · Jun 04, 2013 at 11:45 AM · animationnpc

Problem with animation script for npc

Hello - in my game I need an npc to walk forward for 10 seconds, turn around and then stand idle.

This is the script I have, but the problem is that although the movement forward and turning is correct - he only ever stands in the 'idle' animation, and never 'walk forward'. (I have added them in correctly in the hierarchy)

Do you know if there's anything I'm doing wrong in the script? (JavaScript)

 #pragma strict
 
 public var placeWalkHere : AnimationClip;
 public var placeIdleHere : AnimationClip;
 
 private var walkTime : float = 10;
 private var walkSpeed : float = .01f;
 private var rotationTime : float = 3;
 private var rotationSpeed : float = 1.2;
 
 
 function Start () {
 
 }
 
 function Update () {
     walkTime -= Time.deltaTime;
     if(walkTime > .1f)
     {
         transform.Translate(0, 0, walkSpeed);
         animation.Play(placeWalkHere.name);
     }
     if(walkTime < .1f)
     {
         rotationTime -= Time.deltaTime;
         transform.Rotate(0, rotationSpeed, 0);
     }
     if (rotationTime < .1f)
     {
         rotationSpeed = 0;
         animation.Play(placeIdleHere.name);
     }
 }

Thanks, Laurien

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TonyLi · Jun 04, 2013 at 02:57 PM

Is there something wrong with my animator controller do you think? Or is the script not referencing it properly?

Use the Animator component, not animation.

The animation component [e.g., animation.Play()] is for the Legacy animation system.

Since you're using an animator controller (the newer Mecanim animation system), you need to use the Animator component to control it. Instead of using Play(), you'll need to trigger a transition from your Idle state to your Walk state. I recommend creating a parameter called Speed (or WalkSpeed). When Speed is greater than, say, 0.1, transition from Idle to Walk. When Speed is less than 0.1, transition from Walk to Idle.

In place of Start(), add this:

 private var animator : Animator;
  
 function Start() {
     animator = GetComponent(Animator);
 }

Then replace these lines:

 animation.Play(placeWalkHere.name);
 
 animation.Play(placeIdleHere.name);

With these:

 animator.SetFloat("Speed", 1); //Walk
 
 animator.SetFloat("Speed", 0); //Idle

Comment
Add comment · Show 1 · 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 laurienash · Jun 04, 2013 at 04:41 PM 0
Share

BRILLIANT thanks so much!! I didn't realise about the two different systems! Thanks :)

avatar image
0

Answer by Stormizin · Jun 04, 2013 at 12:00 PM

Dude i think this line is your problem:

 private var walkSpeed : float = .01f;

Change to:

 private var walkSpeed : float = 0.1f;
Comment
Add comment · Show 1 · 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 laurienash · Jun 04, 2013 at 12:56 PM 0
Share

No - I've just changed that, but the character still stays in the idle animation. Is there something wrong with my animator controller do you think? Or is the script not referencing it properly?

avatar image
0

Answer by SubatomicHero · Jun 04, 2013 at 01:41 PM

You could use a state machine into your update function to help you debug what state youre in like so:

 // this script is in c# but you get the idea
 // at the top do this
 enum npcState {
     Walking, Idle, Turning // You could add more if you wish
 };
 npcState myNpcState; // To access the enum
 
 void Start(){
     // setup your default state to start in
     myNpcState = npcState.Walking;
 }
 
 void Update(){
     Debug.Log("NPC State is " + myNpcState);
     switch (myNpcState){
         case npcState.Walking:
             walkTime -= Time.deltaTime;            
             transform.Translate(0, 0, walkSpeed);
             animation.Play(placeWalkNameHere.name);
             if (walkTime < .01f) {
                 myNpcState = npcState.Turning;
             }
             break;
         case npcState.Turning:
             rotationTime -= Time.deltaTime;
             transform.Rotate(0, rotationSpeed, 0);
             walkTime = 0.0f;
             if (rotationTime < .01f) {
                 myNpcState = npcState.Idle;
             }
             break;
         case npcState.Idle:
             rotationSpeed = 0;
             animation.Play(placeIdleHere.name);
             break;
     }
 }
            
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

17 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

Related Questions

How do I add NPCs? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Play animation once 1 Answer

Play animation for horizontal and vertical movement? 1 Answer

Mixamo Call Animation from Script 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