Can't fix this glitch...Please help!

So my player is stuck in idle, he won’t switch to either Walk or Run. Another problem that I have is when i don’t move my players feet are in the ground when I make an movement his feet are on the floor so he glitches. But the main problem is that he won’t switch between animation. I have is setup that if I press W = Walk (speed 4), Shift+W = Run(speed the keys adjust the speed but don’t start a new animation.

My PlayerMovement Script:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

float speed = 8.0f;

Vector3 direction = Vector3.zero;

CharacterController cc;

// Use this for initialization
void Start () {
cc = GetComponent ();
}

// Update is called once per frame
void Update () {
direction = transform.rotation * new Vector3 (Input.GetAxis (“Horizontal”), 0, Input.GetAxis (“Vertical”)).normalized;
if (Input.GetKey (KeyCode.LeftShift)) {
speed = 8.0f;

} else if (!Input.GetKey (KeyCode.LeftShift)) {
speed = 4.0f;
}
}
void FixedUpdate (){
cc.SimpleMove(direction * speed);
}
}

I don’t get any errors just it won’t switch from Idle to either Walk or Run. I have been stuck with this for about 3 days trying to fix this.
(I can’t add the script as an Attachment in this post I have tried but it won’t work I’m sorry for this)

Please, use code tags:
http://forum.unity3d.com/threads/143875-Using-code-tags-properly

And this should usually be posted in the scripting section.

The only thing this script does is to move the player. Though, it is not even necessary to have the movement code in FixedUpdate, you may directly place it in the Update method. Does this movement work or do you already have issues with that?

There are more stuff going on.
First of all, you do “cc = GetComponent ();” inside the “Start()” function and do “cc.SimpleMove(direction * speed);” in “FixedUpdate()”.
Start knows what CC exactly is, FixedUpdate doesn’t.
Put all you have in the Start and FixedUpdate functions in the Update function.
Or at least combine what you have in the Start function with the definition:

CharacterController cc = GetComponent<CharacterController>();

FixedUpdate is only used for rendering physics, anything else goes to the Update function.

As for the controls, it would work better if you turned “Input.GetKey” into “Input.GetKeyDown”.
“Input.GetKeyDown” means “recognise the key the player holds down”, while “Input.GetKey” means “recognise the key the player pressed and assume they released it straight away”.

Note: I did this out of my head right now.

@Blaveloper: there is a definition at the class level for the CharacterController field, so in the initial code posting both the Start and FixedUpdate method are aware of this field. SalvaG’s implementation of GetKey for a check against the movement speed is correct, while the shift key is down increase the speed, using the KeyGetDown would only result in a speed increase for one update iteration.

SalvaG: I don’t see anywhere in your code where you are feeding the speed field into the controllers animator, which if you’re using the Mecanim system, would be the cause of the animation state never changing. Also please note what Dantus stated in regards to the code blocks and proper sub-forums.

So how would I feed the speed to the animator? Like Getcomponent or? (I am using Mecanim)

Thanks now I now how to tag it.

A pretty good example where you can learn a lot is this:
https://www.assetstore.unity3d.com/#/content/14474

There are plenty of tutorials about Mecanim like:
http://www.youtube.com/watch?v=Xx21y9eJq1U

To be honest I watched the tutorial(Before) and I get how to setup the Animator just not the script. So how could I fix my issue?

At least try to create that kind of script, or have a look at the examples assets, because there you can see them in action.
There are not many here in the forum who will code for you, but if you show what you go so far, chances are high, that at least someone will help you.

Yeah I’m trying and I have taken a look at example’s The problem is I don’t see anything wrong with my script xD.

So i tried this I added animation.Play("Run) and animation.Play("Walk)
Now I get another error about that my Player object has no animation atached to it but I added an Animator so it should work right?.

Please watch the video again in order to understand which are the right calls.

animation.Play("Run");

doesn’t work for Mecanim!