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 dyingscreen · Jan 19, 2013 at 05:23 PM · runwalkidleshift

Slow down and play walk animation on left shift?

Sorry for this silly queston but I'm not good at coding at all. I have a very simple 3rd Person controller script, many animations, but so far I've only integrated a 'run' and 'idle.' I know almost nothing about Javascript and I don't plan to know more soon especially because my current project requires very little scripting. I know how to add independant animations like 'bark' or 'sit' on keypress, but I'd love to know how to reduce to 4 speed on my main controller script (below) and crossfade to the walk animation when the left shift is held. This is my script:

 function Update () {
     if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
         animation.CrossFade("Run");
     else
         animation.CrossFade("Idle2");
 }

I'd also like to be able to play random Idle animations at different times, but I know this is another question...

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kru · Jan 20, 2013 at 02:53 AM

 if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
 {
     // decide between run and walk here
     if (Input.GetKey(KeyCode.LeftShift) == true)
     {
         // we're walking
         MyCharacterSpeedVariable = 4;
         animation.CrossFade("Walk");
     }
     else
     {
         // we're running
         MyCharacterSpeedVariable = 10;
         animation.CrossFade("Run");
     }
 }
 else
 {
     animation.CrossFade("Idle2");
 }


Set the speed variable and values to whatever is appropriate for your project.

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 dyingscreen · Jan 20, 2013 at 10:05 AM 0
Share

Hmm, when I place put this script on the character, she still moves but animations don't play at all. Yes, they are all placed in the object, so none are missing... is there somthing I'm doing wrong?

avatar image
0

Answer by simco500 · Jan 20, 2013 at 02:52 AM

The Mecanim tutorial from Unity is perfect for this sort of things. Here it is.

It requires no scripting at all :)

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 dyingscreen · Jan 20, 2013 at 10:05 AM 0
Share

Interesting, I'll have a look at this, it will surely come in useful. c:

avatar image
0

Answer by Chronos-L · Jan 20, 2013 at 02:13 AM

I am not sure why you are going for Mathf.Abs(), but I'm still going to use it because it is your code.

 function Update() {
    if( Mathf.Abs( Input.GetAxis("Vertical") ) > 0.1 ) {
       
       //Input.GetKey() returns true while the key is being hold down
       if( Input.GetKey( KeyCode.LeftShift ) ) {
          animation.CrossFade("Walk");
       }
       else {
          animation.CrossFade("Run");
       }
    }
    else {
       animation.CrossFade("Idle2");
    }
 }



As a bonus, I will propose a solution for your random idle animation

 var idleAnimation : string[] = { "idle1", "idle2" .... };    
 animation.CrossFade( idleAnimation[ Random.Range(0, idleAnimation.Length) ] );

But you will have to figure out how to time the CrossFade() of the idle animations so that you will get a smoother transition between different idle animations. You can use CrossFadeQueue(), but you will have to figure out how to code that part yourself.

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 dyingscreen · Jan 20, 2013 at 10:10 AM 0
Share

This works great, thanks, my only problem is that the speed doesn't change. It could be affected by the third person script I use, which moves my character:

var speed = 6.0; var runSpeed = 9.0; var rotateSpeed = 90;

 var gravity = 20.0;
  
 private var moveDirection = Vector3.zero;
 private var grounded : boolean = false;
  
 function FixedUpdate() {
     if (grounded) {
         // We are grounded, so recalculate movedirection directly from axes
         moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); //Deter$$anonymous$$e the player's forward speed based upon the input.
        
         moveDirection = transform.TransformDirection(moveDirection); //make the direction relative to the player.
         if(Input.GetButton("Jump")) {
             moveDirection *= runSpeed;
         }
         else {
             moveDirection *= speed;
         }
     }
  
     // Apply gravity
     moveDirection.y -= gravity * Time.deltaTime;
    
     // $$anonymous$$ove the controller
     var controller : CharacterController = GetComponent(CharacterController);
     var flags = controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
     transform.Rotate(0, rotateSpeed * Time.deltaTime * Input.GetAxis("Horizontal"), 0);
    
     grounded = (flags & CollisionFlags.CollidedBelow) != 0;
 }
  
 @script RequireComponent(CharacterController)

Sorry it's so long... and thanks a million for the idle script too, I'll play around with that to see if I can get something smooth.

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

12 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

Related Questions

play animation when moving,and idle animation when stand still 1 Answer

Different footstep sounds problem! 2 Answers

Enemy Animation Freezing in first frame 2 Answers

why my Animation "Run" wont work 1 Answer

How do i run scripts more times 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