Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Harardin · Aug 08, 2015 at 11:59 AM · animationcontrollerscriptingbasicsplayer movement

Play animation with FPS controller?

Hi too everyone, I am new at Unity Scripting. I have a problem, I attached standard Unity FPS scripts controller to my player, it works perfectly but I want also enable the animation. When press Key W it will be walkAnimation played and so on. I tried to use this script but it didn’t work and just set the character to 0, 0, 0 axis including the camera. using UnityEngine; using System.Collections;

public class MainControl : MonoBehaviour {

 // Use this for initialization
 void Start () {
     animation.Play("idleAnimation");
     animation.Play("walkAnimation");
 
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.W))
     {
         animation.Play("walkAnimation");

     }
 
 }

}

Maybe I need to use some connections between FPS InputController scripts and play animation script, But I just don’t know how to do it correctly. Sure this question was answered already but I just didn’t find right answer, sorry for asking it again.

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 allenallenallen · Aug 08, 2015 at 12:00 PM 1
Share

There's no need to do anything in your Start(). And try Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)

2 Replies

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

Answer by Harardin · Aug 09, 2015 at 03:43 AM

Here is the deal I try to connect this animation thing with this control script and it didn’t work, can some one please help me to understand why. Alsow script I take at http://wiki.unity3d.com/index.php/RigidbodyFPSWalker using UnityEngine; using System.Collections;

[RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(CapsuleCollider))] public class rigBodyControl : MonoBehaviour { public float speed = 10.0f; public float gravity = 10.0f; public float maxVelocityChange = 10.0f; public bool canJump = true; public float jumpHeight = 2.0f; private bool grounded = false;

 void Awake()
 {
     rigidbody.freezeRotation = true;
     rigidbody.useGravity = false;
     GetComponent<Animator>();
 }

 void FixedUpdate()
 {
     if (grounded)
     {
         // Calculate how fast we should be moving
         Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         targetVelocity = transform.TransformDirection(targetVelocity);
         targetVelocity *= speed;

         // Apply a force that attempts to reach our target velocity
         Vector3 velocity = rigidbody.velocity;
         Vector3 velocityChange = (targetVelocity - velocity);
         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
         velocityChange.y = 0;
         rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
         if (Input.GetKeyDown(KeyCode.W))
         {
             animation.Play("walkAnimation");
             
            // else();
           //  {
            //     animation.Play("idleAnimation");
            // }
         }
         
        
         // Jump
         if (canJump && Input.GetButton("Jump"))
         {
             rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
         }
     }

     // We apply gravity manually for more tuning control
     rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));

     grounded = false;
 }

 void OnCollisionStay()
 {
     grounded = true;
 }

 float CalculateJumpVerticalSpeed()
 {
     // From the jump height and gravity we deduce the upwards speed 
     // for the character to reach at the apex.
     return Mathf.Sqrt(2 * jumpHeight * gravity);
 }

}

Here is the part I actually change: Vector3 velocity = rigidbody.velocity; Vector3 velocityChange = (targetVelocity - velocity); velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange); velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange); velocityChange.y = 0; rigidbody.AddForce(velocityChange, ForceMode.VelocityChange); if (Input.GetKeyDown(KeyCode.W)) { animation.Play("walkAnimation");

            // else();
           //  {
            //     animation.Play("idleAnimation");
            // }
         }

Have to block ELSE part because it don’t let the script run at all. Don’t know maybe I need to add some bool or Float at Animator controller, but don’t know what exactly I need to add.

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 Harardin · Aug 09, 2015 at 11:12 AM 0
Share

Find the solution look here: http://forum.unity3d.com/threads/issue-with-fps-animation.346597/

avatar image
1

Answer by imilanspinka · Aug 08, 2015 at 12:07 PM

Try simply:

 void Update()
 {
     if(Input.GetKey(KeyCode.W))
     {
         Animation.Play("walkAnimation");
     }
     else if(Input.GetKey(KeyCode.S)) //example
     {
         Animation.Play("walkBackwardsAnimation"); //for example
     }
     else
     {
         Animation.Play("idleAnimation");
     }
 }

And one question - I wonder - why are you using animations with a FIRST person controller?

Comment
Add comment · Show 4 · 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 allenallenallen · Aug 08, 2015 at 12:13 PM 2
Share

Although he might just be playing around, I think there are a few perks in having animations. Like seeing your feet move or in multiplayer or having another camera angle that looks at your character in third person. There can legitimate reasons.

avatar image Harardin · Aug 08, 2015 at 08:25 PM 0
Share

Thing is I set up script right it works, but it can’t find the animation files, even if string name set up right

avatar image Harardin · Aug 08, 2015 at 08:42 PM 0
Share

And when press play character snaps to 0, 0, 0, axis if Animator controller is turned on if no everything works good except the animation.

avatar image CaffeineAndCoffee · Nov 06, 2017 at 08:27 PM 0
Share

Animations are quite useful with First person controllers, such as for gun, hand, and leg animations, which greatly increase realism. If you have ever played Call of Duty or Battlefield or any other first person game, you would know that almost all FPSs use animations

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

Can the mecanim animators Controller var be set in code? 1 Answer

Using Override Animation Controllers (NOT WORKING) 0 Answers

Save my WEEK: Converting root motion controller script into jetpack controller script 0 Answers

Move and Animation don't go together (2D) 3 Answers

Create new Mecanim parameter from code? 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