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
-1
Question by Daniel311311 · Sep 13, 2014 at 09:34 PM · c#2dmovement

I need to change the animation when my speed increases in a 2D game

Ok, so I have this script to make my character sprint when shift is held, only for a certain period of time. It works as far as increasing the characters movement speed. Now I need to make it so that I have a variable connected to it in the animator so I can change to my running animation while sprinting.

     `private float movementSpeed;
 //time var values
 private float coolDownTimer;
 private float boostTimer;
 //end time var values
 
 //floats in which we will store the current time left from cooldown timer and boost timer
 private float _boostTimer;
 private float _coolDownTimer;
 //temporary vars to help on the programming
 private bool done;
 private Vector3 movemement;
 private float multiplyer;
 Animator anim;
 
 // Use this for initialization
 void Start () {
     //set the duration for the boostTimer (how long the boost can last) and for coolDownTimer (how long will it take to recharge)
     boostTimer = 6.0f;
     coolDownTimer = 5.0f;
     //set movement speed here, and speedBoost value
     movementSpeed = 3;
     multiplyer = 2.5f; // <-- this right here is the times we multiply our movement speed
     
     done = false;


 }
 
 // Update is called once per frame
 void Update () {


     //we set a temp vector, and initialize it with (0, 0, 0)
     Vector3 movement;
     movement = Vector3.zero;
     
     //we check to see if input keys are pressed, then add the amount to move to the movement Vector3;
     if(Input.GetAxis("Horizontal") != 0){
         movement += transform.right * Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
     }
     if(Input.GetAxis("Vertical") != 0){
         movement += transform.forward * Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
     }



     //we can sprint coz we're moving
     if(movement != Vector3.zero){
         //we check so that we are not on cooldown and if we press shift, we initialize the boostTimer
         if(Input.GetKeyDown(KeyCode.LeftShift) && _coolDownTimer <= 0 && _boostTimer <= 0){
             _boostTimer = boostTimer;
             done = true;
         }
     }
     
     //if we keep the shift pressed and we're not on cooldown, and we still have boosTimer left
     if(Input.GetKey(KeyCode.LeftShift) && _coolDownTimer <= 0 && _boostTimer > 0){
         movement *= multiplyer;                        
     }
     
     //if the boostTimer is finished, initialize the cooldown timer
     if(_boostTimer <= 0 && done){
         _coolDownTimer = coolDownTimer;
         done = false;
     }
     
     
     //we decrease the timers
     _boostTimer -= Time.deltaTime;
     _coolDownTimer -= Time.deltaTime;
     
     
     //if timers go under 0 we set them to be 0, for fun :))
     if(_boostTimer < 0){
         _boostTimer = 0;
     }
     if(_coolDownTimer <= 0){
         _coolDownTimer = 0;
     }    
     
     
     //we apply the movement to the player
     transform.position += movement;
 }
 

}`

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 Baste · Sep 14, 2014 at 12:30 AM 1
Share

What's the problem exactly?

Do you know how the animator works? If not, check out some tutorials. What you need is to have either a blend tree between walking and running in the animator that you send a speed variable to:

 anim.SetFloat("speed", rigidbody.velocity.magnitude")

Or two different states (walk/run) that you switch between with triggers:

 anim.SetTrigger("run");

Both are pretty standard animator things. You should check out the Animation tutorials here, and especially the scripting part here.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Moving object disappears then comes back 1 Answer

Sprite will not roate when arrow keys are pressed 2 Answers

How to know if my player is not moving? 2 Answers

problem with twin stick shooter controls 2 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