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 Jusfowle · Nov 13, 2013 at 11:28 PM · animationmovementcharactercontrollermechanim

Simple NPC Character Movement with Mecanim

Okay, so I followed along with the Mecanim tutorial to set up an idle, running, and attack animation for a character to demonstrate the animations in Unity for a class assignment. However, I do not want the character to be controlled by the player as in the Mecanim tutorial. I was able to use this Root Motion documentation to create a simple movement along the x axis with the following code:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Animator))]
 
 public class RootMotionScript : MonoBehaviour {
 
     void OnAnimatorMove()
     {
             Animator animator = GetComponent<Animator>(); 
 
             if (animator)
             {
            Vector3 newPosition = transform.position;
                newPosition.x += animator.GetFloat("Runspeed") * Time.deltaTime;                                 
            transform.position = newPosition;
             }
     }
 }

This worked but the character clips through the terrain at parts and floats above it at others. I had read that CharacterController can be used to supply the slope navigation I am looking for, but it has no effect with my existing code.

As an alternative, I tried using CharacterController.SimpleMove with the following code:

     var speed : float = 3.0;
     var rotateSpeed : float = 3.0;
 
     function Update () {
         var controller : CharacterController = GetComponent(CharacterController);
 
         // Rotate around y - axis
         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
         
         // Move forward / backward
         var forward : Vector3 = transform.TransformDirection(Vector3.forward);
         var curSpeed : float = speed * Input.GetAxis ("Vertical");
         controller.SimpleMove(forward * curSpeed);
     }
 
     @script RequireComponent(CharacterController)

With this method, the character actually falls to the terrain if placed above it, so I know that vertical movement would work, however it no longer applies the root motion to the animator and therefore will not move in the x axis. I would appreciate any guidance in executing either of these method effectively to have a simple run animation across uneven terrain.

Thank you.

Comment
Add comment · Show 1
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 Jusfowle · Nov 13, 2013 at 11:40 PM 0
Share

When I read about the CharacterController, I didn't realize that you had to apply the Character$$anonymous$$otor script separately. This works with the first set of code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GXMark · Nov 13, 2013 at 11:39 PM

You will run into average velocity and blending issues across the network if using root motion animations over a network. Because unity physics are not deterministic from client to server root blending causes many issues and is a non-trivial issue to solve. My advise if you want to build up a stable networking solution for animation is to use in-place animation sequences.

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

Stop animation when character controller hits walls 1 Answer

Multiple movement modes in mecanim 0 Answers

How to trigger animations in a script using character controller ? 0 Answers

How to move a character with animation on Android? 0 Answers

Animated platform and twitching player 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