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 Ian-McCleary · Aug 28, 2014 at 02:20 AM · animationjavascriptstrafe

How do you add strafe animations to a character?

I know this sounds dumb but i have made all the animations for my character. The last things are the strafe animations (side to side animations when they are only pressing "A" or "D" on the keypad)

The problem is, I don't know where i would add these animations in the character motor. I have a different character motor than originally because i found one that offers more options.

Because im not sure on how to do this exactly in mechanim, i was just going to call the animations using JS when the player pushes one of those buttons. I just dont know when in the script it is called and what to put when it is called.

Help much appreciated here is my function fixedupdate and some of my function start I tried to include as little as possible

 function Start () {
     controller = GetComponent(CharacterController);
     myTransform = transform;
     speed = walkSpeed;
     rayDistance = controller.height * .5 + controller.radius;
     slideLimit = controller.slopeLimit - .1;
     jumpTimer = antiBunnyHopFactor;
     oldPos = transform.position;
 }
  
 function FixedUpdate() {
     var inputX = Input.GetAxis("Horizontal");
     var inputY = Input.GetAxis("Vertical");
     // If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed
     var inputModifyFactor = (inputX != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
  
     if (grounded) {
         var sliding = false;
         // See if surface immediately below should be slid down. We use this normally rather than a ControllerColliderHit point,
         // because that interferes with step climbing amongst other annoyances
         if (Physics.Raycast(myTransform.position, -Vector3.up, hit, rayDistance)) {
             if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
                 sliding = true;
         }
         // However, just raycasting straight down from the center can fail when on steep slopes
         // So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead
         else {
             Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, hit);
             if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
                 sliding = true;
         }
  
         // If we were falling, and we fell a vertical distance greater than the threshold, run a falling damage routine
         if (falling) {
             falling = false;
             if (myTransform.position.y < fallStartLevel - fallingDamageThreshold && enableFallingDamage == true)
                 ApplyFallingDamage (fallStartLevel - myTransform.position.y);
         }
  
         // If running is enabled, change to run speed when left shift is pressed:
         if (Input.GetKey(KeyCode.LeftShift) && enableRun == true)
         {
             speed = runSpeed;
         }
         // If crouching is enabled, change to crouch speed when "c" is pressed:
         else if (Input.GetKey("c") && enableCrouch == true)
         {
             speed = crouchSpeed;
         }
         // If nothing is pressed, use walkSpeed:
         else
         {
             speed = walkSpeed;
         }
  
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
Best Answer

Answer by kacyesp · Aug 28, 2014 at 02:45 AM

  1. Open up the animator (Window -> Animator).

  2. Click on your character in the hierarchy you wish to add animations to.

  3. Now drag your animations into the animator (if they're not already there).

  4. Add transitions between the animations you want to be able to switch from and to (you do this by right clicking on an animation in the animator and you'll see an option to make a transition).

  5. Add a float parameter (bottom left corner of animator window). Call it speed or whatever you want.

  6. Click on the transition arrows you made in step 4 and set the condition to be based on the speed speed parameter you just made. For example, if the speed is greater than some number, you'll want to strafe to the right.

  7. Create an instance variable ( aka field ) of type Animator. In your Start function add:

    animator = GetComponent();

  8. Finally, at the bottom of FixedUpdate, add:

    animator.SetFloat( "speed", speed ); //replace the string with whatever you named the parameter in the animator

Comment
Add comment · Show 6 · 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 Ian-McCleary · Aug 28, 2014 at 03:21 AM 0
Share

Believe it or not i have already done this with the walk and run animation. I named it exactly "Speed" I assume this means i should create a new float parameter named "SideSpeed" or something. What im not sure of however is where to say "not sure of function" = "SideSpeed" Currently i am calling normal speed from a different script (it would make more sense to call them both from the same script, but i thought it would be easier to figure it out in the character motor) Here is the current "Speed" calling i have.

     void FixedUpdate ()
     {
         float h = Input.GetAxis("Horizontal");                // setup h variable as our horizontal input axis
         float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis
         //anim.SetFloat("Speed", v);                            // set our animator's float parameter 'Speed' equal to the vertical input axis                
         //anim.SetFloat("Direction", h);                         // set our animator's float parameter 'Direction' equal to the horizontal input axis        
         anim.speed = animSpeed;                                // set the speed of our animator to the public variable 'animSpeed'
         anim.SetLookAtWeight(lookWeight);                    // set the Look At Weight - amount to use look at I$$anonymous$$ vs using the head's animation
         currentBaseState = anim.GetCurrentAnimatorStateInfo(0);    // set our currentState variable to the current state of the Base Layer (0) of animation
         
         if(anim.layerCount ==2)        
             layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);    // set our layer2CurrentState variable to the current state of the second Layer (1) of animation


Would i create a new function or something? What deter$$anonymous$$es the side movement?

avatar image Ian-McCleary · Aug 28, 2014 at 03:39 AM 0
Share

Its commented out because i was messing around on a different script.

avatar image kacyesp · Aug 28, 2014 at 03:59 AM 0
Share

I think I ran into a similar issue yesterday of "Where do I update animations?" $$anonymous$$y solution was to have the motors/controllers update a PlayerState class/struct and then make an AnimationController to update the flags for the animator based on the values set in the same PlayerState. When you know the player is strafing, then set an IsStrafing boolean to true in the character motor. Then the AnimationController will update the Animator's parameters. Does that answer your question?

avatar image Ian-McCleary · Aug 28, 2014 at 04:29 AM 0
Share

So you think i should create like a whole new script for animations and say something like "if(Input.GetkeyDown(keycode.D))set like a parameter thats 1 through -1 and say -1 strafes to the left and 1 strafes to the right? I know you said boolean but im not 100% sure i would know how to do it with a boolean.

avatar image Ian-McCleary · Aug 28, 2014 at 03:57 PM 1
Share

Haha thanks for the great idea! I will try it out today! Thanks!

Show more comments

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

23 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

Related Questions

How to open a door with the "E" key 1 Answer

How would you reccommend adding the jumping animation to my script? 0 Answers

Texture Swap + Animated Offset/Tiling 1 Answer

Rotating An Object On Its Axis Once 3 Answers

Animation play when dead 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