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.
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?
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.