- Home /
Animation problem
Hey guys ;)
I wanna do simple animations (walk, run, jump).
I think that the script part is correct, but once I saved the script, I have an error:
Assets/Standard Assets/Scripts/Camera Scripts/Movecharacter.js(51,42): BCE0019: 'animation' is not a member of 'Object'.
Here is the script:
#pragma strict
var speed:float;
var speedRun:float;
var speedRotate:float;
var gravity:float;
var jumpspeed:float;
private var controller:CharacterController;
private var moveDirection:Vector3;
private var deltaTime:float;
private var characterContent;
private var runAnim:boolean;
private var run:boolean;
function Start () {
controller = GetComponent("CharacterController");
characterContent = transform.Find("samuzai");
}
function Update () {
deltaTime = Time.deltaTime;
runAnim = false;
if(controller.isGrounded){
if(Input.GetKey(KeyCode.LeftShift)){
moveDirection = Vector3(0,0,Input.GetAxis("Vertical") * speedRun);
runAnim = true;
}
else{
moveDirection = Vector3(0,0,Input.GetAxis("Vertical") * speed);
}
if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow)){
if(!runAnim){
characterContent.animation.CrossFade("walk", 0.2);
}
else{
characterContent.animation.CrossFade("run", 0.2);
}
}
else{
characterContent.animation.CrossFade("idle", 0.2);
}
if(Input.GetKey(KeyCode.Space)){
moveDirection.y = jumpspeed;
characterContent.animation.CrossFade("jump", 0.2);
}
}
moveDirection = transform.TransformDirection(moveDirection);
transform.Rotate(Vector3(0,Input.GetAxis("Horizontal") * speedRotate * deltaTime,0));
moveDirection.y -= gravity * deltaTime;
controller.Move(moveDirection * deltaTime);
}
Thanks :)
Answer by AeonIxion · Nov 17, 2013 at 10:09 PM
I don't know much about javascript..but try changing private var characterContent;
to private var characterContent:Transform;
.
Answer by Arpatos · Nov 18, 2013 at 08:36 PM
Thanks I have no errors for the script now, but anyways, if I'm trying to jump for example, it tolds me :
The animation state jump could not be played because it couldn't be found! Please attach an animation clip with the name 'jump' or call this function only for existing animations. UnityEngine.Animation:CrossFade(String, Single) Movecharacter:Update() (at Assets/Standard Assets/Scripts/Camera Scripts/Movecharacter.js:51)
Any idea ?
You need to add the animations to the Animation component in the inspector.
You are accessing the animation component of characterContent. characterContent is the result of Transform.find("samuzai"). The animation component is attached to "samourai". So changing Transform.find("samuzai")
to Transform.find("samourai") should work.
Now I have another error xD
There is it : NullReferenceException $$anonymous$$ovecharacter.Update () (at Assets/Standard Assets/Scripts/Camera Scripts/$$anonymous$$ovecharacter.js:46)