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 /
avatar image
0
Question by gagannagpal101 · Feb 28, 2019 at 03:43 PM · animationrunblend treewalk

How do i make my player go from walk animation to run animation using blend trees?

I need help going from the walk animation to run animation with blend. when i press left shift the character walks faster but when i click left shift i also want the run animation to work, this is my script & blend tree:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerController : MonoBehaviour

{

 private float currentMaxSpeed = 0;
 [SerializeField]
 private Animator anim;
 [SerializeField]
 private float walkSpeed;
 [SerializeField]
 private float runSpeed;

 void Start()
 {
     anim = GetComponent<Animator>();
 }

 void Update()
 {

     if (Input.GetKey(KeyCode.LeftShift))
     {
         currentMaxSpeed = runSpeed;
     }
     else
     {
         currentMaxSpeed = walkSpeed;
     }
     Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);

     anim.SetFloat("Horizontal", movement.x);
     anim.SetFloat("Vertical", movement.y);
     anim.SetFloat("Magnitude", movement.magnitude);

     transform.position = transform.position + movement * Time.deltaTime * currentMaxSpeed;
 }

} alt text

blend-tree.png (190.6 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

2 Replies

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

Answer by Skaster87 · Feb 28, 2019 at 05:08 PM

I'm going to assume this is a top down game with y and x being the ground plane and z is height?

The easiest solution I can suggest is a debug.log( movement ) to find the highest values that each speed produces, and then set the blend tree float to just above the walk speed range.
If you always want the run to trigger when the shift key is pressed, I would just assign the run speed value if the shift key is pressed in the update loop.

  float speed = 1;
     void Update()
     {
         if (Input.GetKey(KeyCode.LeftShift))
         {
            speed = 2;
          } 
         else
         { speed = 1;}
       
         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
         anim.SetFloat("Horizontal", movement.x * speed);        
         anim.SetFloat("Vertical", movement.y * speed);  
         anim.SetFloat("Magnitude", movement.magnitude);
         transform.position = transform.position + movement * Time.deltaTime * speed;
     }

I would then use a single 2d blend tree with the walk animations and the run animations using x & y,

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

Answer by gagannagpal101 · Feb 28, 2019 at 06:11 PM

i did increase the run speed to 1.1 in blend tree and replaced that in my script but then the left, down animation stops working and when i release left shift it still walks fast, and if i hold it down longer it increases the speed, even letting go will keep that speed, have you got discord or skype?

Here is a clip of the problem: https://ufile.io/9ca8q

Here is a copy of my game: https://ufile.io/2gf5i @Skaster87

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 Skaster87 · Mar 01, 2019 at 12:23 AM 0
Share

I updated the code to address your issue, but you'll need to address each animation in the blend tree. @gagannagpal101

avatar image gagannagpal101 Skaster87 · Mar 02, 2019 at 03:25 PM 0
Share

Thanks Alot!

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

258 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

Related Questions

idle > walk > run animation 1 Answer

Enemy animtion and movement 0 Answers

Locomotion System (By Rune Skovbo Johansen) 1 Answer

why my Animation "Run" wont work 1 Answer

Lerpz ThirdPersonPlayerAnimation bug - running at walking speed 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