Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Diamondmeh000 · Dec 26, 2020 at 07:47 AM · programming

how to get walking animation to stop playing

I am trying to get this character to stop playing its animation when you release the w, a, s, or d key but now when I am switching from one key to another the animation won't play. here is the script

 void Update()
 {
     if(Input.GetKeyDown("w"))
     {          
         anim.Play ("BasicMotions@Walk01"); 
     }

      if (Input.GetKeyUp("w"))
      {
          
          anim.Play ("Idle ");
         
      }

      if (Input.GetKeyUp("a"))
      {
          
          anim.Play ("Idle ");
         
      }
      if (Input.GetKeyUp("s"))
      {
          
          anim.Play ("Idle ");
         
      }
      if (Input.GetKeyUp("d"))
      {
          
          anim.Play ("Idle ");
         
      }
     
     if(Input.GetKeyDown("a"))
     {     
         anim.Play ("BasicMotions@Walk01");           
     }

    
      if(Input.GetKeyDown("d"))
     {          
         anim.Play ("BasicMotions@Walk01");           
     }

     
       if(Input.GetKeyDown("s"))
     {      
              
         anim.Play ("BasicMotions@Walk01");           
     }
 }

} ` can I please have some help, thanks!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by prakyathd801 · Dec 31, 2020 at 05:44 AM

Try replacing your code with below code @Diamondmeh000

  anim.Play ("AnimationName", -1, 0); 


Comment
Add comment · Show 1 · 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 prakyath_unity · Dec 31, 2020 at 05:51 AM 0
Share

Thank you! This works like a charm

avatar image
0

Answer by OGOUTIS · Dec 26, 2020 at 10:11 AM

This should work

 if(Input.GetKey("w") ||Input.GetKey("a") ||Input.GetKey("s") ||Input.GetKey("d"))
 {
     anim.Play("BasicMotions@Walk01");
 }
 else
 {
     anim.Play("Idle");
 }

if there is no w,a,s,d input , your character will play idle animation and as you hold down these keys your character will play walk animation

Comment
Add comment · Show 3 · 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 Diamondmeh000 · Dec 27, 2020 at 01:27 AM 0
Share

This does not work, the character just zaps to walking and then back to idle.

avatar image Diamondmeh000 · Dec 27, 2020 at 09:11 AM 0
Share

I have already got a movement script but I am making a seperate one for animations

avatar image OGOUTIS Diamondmeh000 · Dec 27, 2020 at 10:47 AM 0
Share

That's strange.Are you sure you typed Input.GetKey instead of Input.GetKeyDown or anything Because GetKey returns the statement true as long as you press the key but the GetKeyDown returns statement true only on the frame you start pressing.

avatar image
0

Answer by $$anonymous$$ · Dec 26, 2020 at 01:26 PM

Use this

 private Animator anim;
 
 private void Start (){
         // Gets the Animator Component in the gameObject that have this script
         anim = GetComponent<Animator>();
 }
 
 private void Update (){
          // Gets the Input of the w,a,s,d/up,left,right,down arrows
          Vector2 moveInput = new Vector2 (Input.GetAxisRaw(Horizontal),. 
          (Input.GetAxisRaw(Vertical);
          
          // Make a float parameter called "Speed" In the Animator Component
          anim.SetFloat("Speed", moveInput.magnitude);
 
         // If you do not know how to use the Animator Component post a 
         comment
 }
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 Diamondmeh000 · Jan 02, 2021 at 07:00 AM

this answer is not really an answer I just needed the code to be formatted properly. I realised that I did type Input.GetKeyDown instead of Input.GetKey and that fixed the problem but now my movement script is not working, when I press W, a, s, or d it zaps to a random position here it is

 public class ThirdPersonScript : MonoBehaviour  {

 public Transform cam;

 public CharacterController controller;

 public float speed = 6f;

 public float turnSmoothTime = 0.15f;
 float turnSmoothVelocity;

 void Start ()

{ Cursor.visible = false; Screen.lockCursor = true; }

 void Update()
 {
     float horizontal = Input.GetAxisRaw("Horizontal");
     float vertical = Input.GetAxisRaw("Vertical");
     Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

     if(direction.magnitude >= 0.1f)
     {

         float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
         float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
         transform.rotation = Quaternion.Euler(0f, angle, 0f);

         Vector3 movedirection = Quaternion.Euler(0f, targetAngle, 0f) *Vector3.forward;
         controller.Move(movedirection.normalized * speed * Time.deltaTime);
     }

 }

} ` can I have some help please because this is extremely annoying any help would be appreciated

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

128 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

Related Questions

Multiple Cars not working 1 Answer

Inheritance in C# and Unity3d 1 Answer

Mouse targeting error 1 Answer

How do I seed the random number generator myself and still get pseudorandom values? 2 Answers

How much do Unity programmers charge/hour? 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