I want to use the new anim graph.I have tried the tutorial like 30 times but it still doesn’t work.
Can someone take this code and make it work with the new anim compotent.
var AnimationController = pc.createScript('animationController');
AnimationController.attributes.add("iddleSpeed", {type: "number"});
AnimationController.attributes.add("runSpeed", {type: "number"});
AnimationController.attributes.add("blendTime", {type: "number"});
AnimationController.states = {
idle: {
animation: 'idle.glb'
},
run: {
animation: 'Run.glb'
},
crouch: {
animation: 'CrouchIdle.glb'
},
crouchwalk: {
animation: 'CrouchWalk.glb'
},
};
// initialize code called once per entity
AnimationController.prototype.initialize = function() {
this.setState('idle');
this.isMove = false;
};
AnimationController.prototype.setMovement = function(isMove) {
if(isMove != this.isMove){ // the state is new
this.isMove = isMove;
if(isMove){
this.setState('run');
}
else{
this.setState('idle');
}
}
};
AnimationController.prototype.setState = function (state) {
var states = AnimationController.states;
this.state = state;
// Set the current animation, taking 0.2 seconds to blend from
// the current animation state to the start of the target animation.
if(this.state == "idle" || this.state == "crouch"){
this.entity.animation.speed = this.iddleSpeed;
}
else{
this.entity.animation.speed = this.runSpeed;
}
this.entity.animation.play(states[state].animation, this.blendTime);
};
Hi @Connor_Briggs! Which tutorial project did you use? There could be a lot of reasons why your animation is not working. For example, I see you acces a animation component in your script while the new animation system use the anim component.
both the third person controller tutorial and anim graph tutorial
if i don’t reply back, I am going to karate
Right. Please check which component you use on your character. It should be the anim component. Update your script with the correct code and make sure you use GLB files instead of JSON files.
1 Like
Hi @Connor_Briggs,
This method will not work with the new anim component:
this.entity.anim.play('Idle');
That’s why you are getting an error. You need to get a reference to an animgraph layer to call play(), if you actually need to do that. Check the updated third person tutorial on how that works:
https://playcanvas.com/editor/scene/961236
1 Like
It still isn’t working
Animgraph.prototype.setMovement = function(isMove) {
if(isMove != this.isMove){ // the state is new
this.isMove = isMove;
if(isMove){
this.entity.anim.play('Walk');
}
else{
this.entity.anim.play('Idle');
}
}
};
Yeah, that won’t work, check my reply above, there is no anim.play()
method.
Study carefully the third person controller example on how to use animgraph.
1 Like
Can you share a project link please?
1 Like
Edited my first message in the thread, sorry @Connor_Briggs , I meant that method won’t work with the anim component.
The code i need help with is the animgraph script
Please try to keep your posts in one post as much as possible @Connor_Briggs. If you want to add something you can edit the post with the pencil icon below the post.
2 Likes
The first thing you need to do is to decide how you want the animations to switch. For this you need to create a parameter in the anim state graph. I suggest using an integer. This is a number where for example, 0
stands for idle, 1
for move forward and -1
for move backwards. You also need to add for every state this parameter as a condition to your transition. Then you can change the parameter in the script at the right time so the right transition can be executed.
1 Like
Okie how do you do that? I think i have a anim state grapg it is the coding i am confusted on
could you give me an example
in code form
Below some examples how to do this.
-
Create the integer.
-
Create the condition for every transition.
-
Set the integer by script.
if (isMove) {
this.entity.anim.setInteger("State", 1);
}
else {
this.entity.anim.setInteger("State", 0);
}
2 Likes
thanks i’ll try that and see if it works