Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Blueneonkid · Dec 19, 2016 at 07:38 PM · c#scripting problemmovementanimator controllerdiagonal

How do I get diagonal 3d movement?

I have a model moving using root motion. Currently, it's moving in 4 directions; Forward, Backward, Right, and Left.

Now, I want to make the character move in 45 degrees. I made a chart to understand the axes. alt text

The character has a running animation for every direction, including the diagonal directions. Here is my animator controller set up (image link because I can't have any more attatchments): http://orig10.deviantart.net/082a/f/2016/354/c/3/zanimator_by_blueneonkid-das9mji.png

I started out with trying to move diagonally left, and tried out a few scripts.

Script #1 (The simplest):

 public class Zcontroller : MonoBehaviour {
     
     private Animator myAnimator;
 
     // Use this for initialization
     void Start () {
         myAnimator = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         myAnimator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));
         myAnimator.SetFloat ("HSpeed", Input.GetAxis ("Horizontal"));
         myAnimator.SetFloat ("DiagSpeed", Input.GetAxis ("Vertical") + Input.GetAxis ("Horizontal"));

Script #2:

 using UnityEngine;
 using System.Collections;
 
 public class Zcontroller : MonoBehaviour {
     
     private Animator myAnimator;
 
     // Use this for initialization
     void Start () {
         myAnimator = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () {
         myAnimator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));
         myAnimator.SetFloat ("HSpeed", Input.GetAxis ("Horizontal"));
  
 
         if (Input.GetAxis ("Vertical") || Input.GetAxis ("Horizontal")) {
 
             if ((Input.GetAxis ("Vertical") < 0f) && (Input.GetAxis ("Horizontal") > 0)) {
                 myAnimator.SetBool ("isDiagonalLeft", true);
             }
 
         } else {
             myAnimator.SetBool ("TurningLeft", false);
         }
     }
 }

Script #3 (As an image link because I forgot to save this one and can't add any more attachments): alt text

Results:

Script 1 did not work, and caused the character to move diagonally only when the Down Arrow key was pressed.

Script 2 also did not work, giving me the error: "Operator '||' cannot be applied to operands of type 'float' and 'float'."

Script 3 gave me some bracket errors.

What can I do to get the character to move diagonally properly?

Is there any way to combine two float values in code? If so, how?

zcontroller-error-1-whited-out.png (27.1 kB)
unity-movement-chart.png (11.0 kB)
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 UnityCoach · Dec 19, 2016 at 08:07 PM

All you need is in the 2D Blend Tree in Animator. You simply expose X and Y parameters from the Animator Controller, which you manipulate from the code, like you do. Then, you add you diagonal animations to the Blend Tree and position them on the 2D graph.

Comment
Add comment · Show 7 · 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 Blueneonkid · Dec 19, 2016 at 10:07 PM 0
Share

"Expose the X and Y parameters"? Could you explain this a bit more, please? I'm a noob.

avatar image UnityCoach Blueneonkid · Dec 19, 2016 at 10:14 PM 0
Share

Well, I see you already set float parameters like this :

 myAnimator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));
          myAnimator.SetFloat ("HSpeed", Input.GetAxis ("Horizontal"));

I assume you already have those parameters "exposed" in the Animator Controller, right?

If so, all you need is to create a 2D blend tree and add all the different animations to it.

avatar image Blueneonkid UnityCoach · Dec 19, 2016 at 10:28 PM 0
Share

Yeah, I have those in the animator controller. Setup: alt text

However, it still doesn't play the diagonal run animation whenever I hold two directional keys down.

I set the Diagonal Left animation to -1, 1 and the Diagonal Right animation to 1,1

zanimator-setup-2.png (119.5 kB)
Show more comments
avatar image Blueneonkid · Dec 19, 2016 at 10:59 PM 0
Share

Yes, there are no other layers on top. Buuuut, its' set up like this: alt text

Is this set up causing my problem? (For some reason, I can't reply to your post)

avatar image UnityCoach Blueneonkid · Dec 20, 2016 at 07:51 PM 0
Share

Yes, I know, you can only reply to post I published to you. I can't see the image.

avatar image Blueneonkid UnityCoach · Dec 22, 2016 at 04:34 AM 0
Share

I made a forum topic so you can help me better: https://forum.unity3d.com/threads/how-do-i-get-diagonal-3d-movement-with-root-motion.447057/ Also, here's the image: alt text

zanimator-annotated.png (47.8 kB)

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

296 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to tilt a sprite based on pointer position? 1 Answer

how do you fix error cs0426 the nested type 'module' does not exist in the type 'unityengine.particalsystem'"? 0 Answers

Input.GetAxisRaw always returns -0.6 1 Answer

Movement Sticking 0 Answers

Hexagonal Grid Rangefinding 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