- Home /
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.
There's no need to do anything in your Start(). And try Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)
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.
Find the solution look here: http://forum.unity3d.com/threads/issue-with-fps-animation.346597/
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?
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.
Thing is I set up script right it works, but it can’t find the animation files, even if string name set up right
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.
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