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 SuperSparkplug · Sep 20, 2013 at 06:54 AM · animationplayermecanimdirection

How do I make Mecanim animate on local Z direction?

Hi, so I have a character I made in Maya and imported to Unity with animations and all. I'm just learning Mecanim and doing some testing. I have my character moving and am trying to understand it. However, I wanted him always facing the direction he's going, which he wasn't. On Unity Answers, I found this handy piece of code that makes my character always face the direction the player presses.

             transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"))); // Normalizes the rotation of Z local while moving

So now while he faces the direction of whatever direction I press, he only animates when I press forward. If I press any other direction, he moves in that direction, but the animation stops. Also, if the player is no longer pressing that direction, the model instantly snaps to his native Z direction. To be honest, I don't quite understand Mecanim completely just yet, though I've been doing a Digital Tutors tutorial on the subject to gain some understanding. Even so, while I am understanding the gist of the code and methods, I'm having trouble pinpointing where my script accesses the walk blendtree of mecanim. To gain an understanding of my problem, here is a picture of some test models I made for my Unity scene with my character and I want him to move freely around the room.

alt text

I'm guessing the animation has something to do with the following section of my code. Does anyone know how to solve this and what I'm doing wrong?

     void Start ()
     {
         // initialising reference variables
         anim = GetComponent<Animator>();                                          
         if(anim.layerCount ==2)
             anim.SetLayerWeight(1, 1);
     }
     
     void OnAnimatorMove() //Tells Unity that root motion is handled by the script
     {
         if(anim)
         {
             Vector3 newPosition = transform.position;
             newPosition.z += anim.GetFloat("speed")* meshMoveSpeed * Time.deltaTime;
             newPosition.x += anim.GetFloat("direction") * meshMoveSpeed * Time.deltaTime;
             transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"))); // Normalizes the rotation of Z local while moving
             transform.position = newPosition;
             
         }
     }
     
     
     void FixedUpdate ()
     {
         float h = Input.GetAxis("Horizontal");                // setup h variable as our horizontal input axis
         float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis
         anim.SetFloat("speed", v);                            // set our animator's float parameter 'Speed' equal to the vertical input axis                
         anim.SetFloat("direction", h);                         // set our animator's float parameter 'Direction' equal to the horizontal input axis        
         anim.speed = animSpeed;                                // set the speed of our animator to the public variable 'animSpeed'
         currentBaseState = anim.GetCurrentAnimatorStateInfo(0);    // set our currentState variable to the current state of the Base Layer (0) of animation


         
 


screen shot 2013-09-20 at 2.37.50 am.png (249.1 kB)
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 manikantlawania · Jun 04, 2015 at 03:20 PM 0
Share

did you find a solution man ?

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

Third-person character animations based on World-relative input and Y-rotation (looking at mouse position) 1 Answer

I have a value that determines which way my character is facing in degrees. How do I convert that value to a range of -1 to 1 for smooth mecanim bleend tree animation? 0 Answers

How do I access the "Average Velocity" of a run animation? 1 Answer

free motion in mecanim 0 Answers

Player look in camera direction problem 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