Ok, so I’m trying to script my own controls for the player in my game. I’m pretty new to JavaScript and Unity, having only worked with them for less than 6 months, but I catch on pretty fast.
The Problem:
While I have the character moving with the correct buttons, I’m having difficulty with making the animations run appropriately. I had a script that worked, but it was ridiculously long and I thought I could do it an easier way, which is what I’m looking for. I’d hold down W to move forward, and the character (named Zikku) would move forward and animate running forward. However, upon releasing W, she would continue to loop in her running animation instead of idle. Again, this worked in the long code I have comment blocked out, but not the simple code that I’m trying to get. Is there something about switch statements that I’m missing?
Current Code:
var zikkuChar: GameObject; var
runSpeed: int;
var runBackSpeed: int;
var turnSpeed: int;
var jumpHeight = 10;//How high I jump after pressing
the space bar var gravity = 20; var
canJump = true;
var zikkuKeys: Array;
enum ZikkuState
{
Idle = 0,
Running = 1,
PivotLeft = 2,
PivotRight = 3,
Jumping = 4,
Attacking = 5,
MoveBackwards = 6,
}
private var _zikkuState : ZikkuState;
function Start()
{
zikkuKeys = new Array();
zikkuKeys.Push(KeyCode.W);
zikkuKeys.Push(KeyCode.A);
zikkuKeys.Push(KeyCode.S);
zikkuKeys.Push(KeyCode.D);
}
function Update()
{
renderer.enabled=false;
jumpPlayer = (Vector3.up*Time.deltaTime);
ApplyGravity ();
ApplyJumping ();
for (var i: KeyCode in zikkuKeys)
{
if(Input.GetKey(i))
{
switch(i)
{
case KeyCode.W:
PerformSomething(ZikkuState.Running);
break;
case KeyCode.A:
PerformSomething(ZikkuState.PivotLeft);
break;
case KeyCode.S:
PerformSomething(ZikkuState.MoveBackwards);
break;
case KeyCode.D:
PerformSomething(ZikkuState.PivotRight);
break;
default:
PerformSomething(ZikkuState.Idle);
break;
}
}
}
/*if(Input.GetKey("w"))//If w is held down, move Zikku forward
{
transform.Translate(movePlayer * runSpeed);
}
if(Input.GetKeyDown("w"))//If w was pressed, play running animation
{
zikkuChar.animation.CrossFade("run", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
}
if(Input.GetKeyUp("w"))//If w was released, play idle animation
{
zikkuChar.animation.CrossFade("idle", 0.2);
}
if(Input.GetKey("a"))//If a is being held down, turn Zikku to the left
{
transform.Rotate(-turnPlayer * turnSpeed);
}
if(Input.GetKeyDown("a"))//If a was pressed, then...
{
if(!Input.GetKey("w" || "s"))//If w and s are not being pressed then play animation to turn Zikku left
{
zikkuChar.animation.CrossFade("pivotLeft", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
}
}
if(Input.GetKeyUp("a"))//If a was released, play idle animation
{
zikkuChar.animation.CrossFade("idle", 0.2);
}
if(Input.GetKey("s"))//If s is being held down, move Zikku backwards
{
transform.Translate(-movePlayer * runBackSpeed);
}
if(Input.GetKeyDown("s"))//If s was pressed then play backwards animation.
{
zikkuChar.animation.CrossFade("moveBackward", 0.2);
}
if(Input.GetKeyUp("s"))//If s was released, then play idle animation.
{
zikkuChar.animation.CrossFade("idle", 0.2);
}
if(Input.GetKey("d"))
{
transform.Rotate(turnPlayer * turnSpeed);
}
if(Input.GetKeyDown("d"))
{
if (!Input.GetKey("w" || "s"))
{
zikkuChar.animation.CrossFade("pivotRight", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
}
}
if(Input.GetKeyUp("d"))
{
zikkuChar.animation.CrossFade("idle", 0.2);
}*/
}
function PerformSomething(currentState : ZikkuState)
{
movePlayer = (Vector3.forward*Time.deltaTime);
turnPlayer = (Vector3.up*Time.deltaTime);
switch(currentState)
{
case ZikkuState.Idle:
zikkuChar.animation.CrossFade("idle", 0.2);
break;
case ZikkuState.Running:
transform.Translate(movePlayer * runSpeed);
zikkuChar.animation.CrossFade("run", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
break;
case ZikkuState.PivotLeft:
transform.Rotate(-turnPlayer * turnSpeed);
zikkuChar.animation.CrossFade("pivotLeft", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
break;
case ZikkuState.MoveBackwards:
transform.Translate(-movePlayer * runBackSpeed);
zikkuChar.animation.CrossFade("moveBackward", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
break;
case ZikkuState.PivotRight:
transform.Rotate(turnPlayer * turnSpeed);
zikkuChar.animation.CrossFade("pivotRight", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
break;
//case ZikkuState.Jumping:
//jumping stuff
//break;
//case ZikkuState.Attacking:
//attacking stuff
//break;
}
}
I don’t have the attack animation set up yet, and I’m not worrying myself with jumping just yet either. Any detailed help would be appreciated since I’m not too good with this stuff (: Thanks!
Edit: I suck with using that code snippets button >_>