Hi all. I’m fairly new to JMonkey. Currently I have two ogre spatials and I’m adding a ninja spatial. I have a characterControl attached to the ninja. When I do setWalkDirection or jump on the ninja’s characterControl, I don’t get any changes in the ninja’s location as I do with the enemy ogre. Much appreciated if someone can help me out! XD
repasted below with code format
And one other thing I noticed is that the viewing direction of a ninja’s character control is exactly opposite that faced by the ninja spatial. This is exactly on the contrary to an ogre where the viewing direction of a character control matches the direction the ogre spatial faces. Anyone know if this is the way the ninja should be? :-o
When you paste code, could you please use the “code” blocks so that its syntax is highlighted? Reading plain-text code is painful… Especially when there are pages of it…
This is how I initialized my ninja
[java]CapsuleCollisionShape capsuleShapeNinja = new CapsuleCollisionShape(1f, 6f, 1);
ninja = new CharacterControl(capsuleShapeNinja, 0.05f);
ninja.setJumpSpeed(20);
ninja.setFallSpeed(30);
ninja.setGravity(30);
avatarNinja = (Node) assetManager.loadModel("Models/Ninja/Ninja.mesh.xml");
avatarNinja.scale(0.031f, 0.031f, 0.031f);
avatarNinja.setLocalTranslation(15.0f, 1.0f, -7.0f);
turn = new Quaternion();
turn.fromAngleAxis(180f * FastMath.DEG_TO_RAD, Vector3f.UNIT_Y);
avatarNinja.rotate(turn);
avatarNinja.addControl(ninja);
rootNode.attachChild(avatarNinja);
controlNinja = avatarNinja.getControl(AnimControl.class);
controlNinja.addListener(this);
channelMotionNinja = controlNinja.createChannel();
channelAttackNinja = controlNinja.createChannel();
channelMotionNinja.setAnim("Idle1", 0.5f);[/java]
This is in simpleUpdate where the ninja is moved around
[java]if(rootNode.hasChild(avatarNinja)) // the ninja is alive
{
if(rootNode.hasChild(avatarEnemy)) // the enemy is alive
{
ninja.setViewDirection(new Vector3f(enemy.getPhysicsLocation().x - ninja.getPhysicsLocation().x, 0f, enemy.getPhysicsLocation().z - ninja.getPhysicsLocation().z)); // the ninja looks horizontally at the enemy’s location
ninja.setWalkDirection(ninja.getViewDirection().normalize().mult(0.06f));
}
}
System.out.println(ninja.getPhysicsLocation());[/java]
facepalm
I did the same as with the ninja as I did with the ogres, but now the ninja doesn’t jump or move around like the ogres do and also the ninja’s character control’s view direction is opposite to the direction the ninja spatial faces. Any idea what I might be doing wrong? :-o
Okay, I just replaced the ninja spatial by an ogre spatial and the same thing happens: setViewDirection works but setWalkDirection and jump do not. I’ll looking into it further.
Oops, didn’t get my 3rd sprite to bulletAppState. All good now with adding a third ogre :lol: Now I try again with adding a ninja to see why the character control’s view direction is opposite the direction faced by the spatial.
Anyone know the name of the animation where the ninja stands rather than crouches as with the idle animation? Thanks in advance!
How about looking at the animations (and their names) in the SceneComposer?
Okay, now I got my ninja working except for 1 part. When the ninja is close enough to the enemy ogre, it stops when I set it to Idle1, but I can’t get it to attack using Attack1 even though I can get it to jump. Below is my code. Whenever the counter n gets to 400, the ninja should jump and attack. So far, the ninja jumps which is line 25, but it does not do the attack motion which are lines 26 and 27. Help on this last part would be greatly appreciated
[java]// manage the ninja
if (rootNode.hasChild(avatarNinja)) // the ninja is alive
{
if (rootNode.hasChild(avatarEnemy)) // the enemy is alive
{
if (ninja.getPhysicsLocation().distance(enemy.getPhysicsLocation()) 6f) // the ninja walks towards the enemy
{
ninja.setWalkDirection(ninja.getViewDirection().mult(-1f).normalize().mult(0.05f));
if (ninja.getPhysicsLocation().distance(enemy.getPhysicsLocation()) > 6f)
{
if (!channelMotionNinja.getAnimationName().equals("Walk"))
{
channelMotionNinja.setAnim("Walk", 0.5f);
}
}
}
else // the ninja is within attacking range
{
ninja.setWalkDirection(Vector3f.ZERO);
n ++;
if (n == 400) // the ninja attacks
{
ninja.jump();
channelAttackNinja.setAnim("Attack1", 0.5f);
channelAttackNinja.setLoopMode(LoopMode.DontLoop);
n = 0;
}
else
{
channelMotionNinja.setAnim("Idle1", 0.5f);
}
}
}
else // the ninja does not notice the enemy
{
ninja.setWalkDirection(Vector3f.ZERO);
channelMotionNinja.setAnim("Idle1", 0.5f);
}
}
}
// done managing the ninja[/java]
If I comment line 32 and replace channelAttackNinja by channelMotionNinja, the ninja begins to continuously attack when the counter n hits 400. How come the don’t loop command has no effect? :-/
What I found is that when the ninja’s current animation is Idle1, I cannot reset its animation except for jumping. For example, if I have the following:
channelMotionNinja.setAnim(“Idle1”);
channelAttackNinja.setAnim(“Attack1”);
Then my ninja only does Idle1.
Anyway around this? Much appreciated
I used onAnimCycleDone and solved the problem XD. However, if I have the following in simpleUpdate
[java]
else // the ninja is within attacking range
{
ninja.setWalkDirection(Vector3f.ZERO);
n ++;
if (n == 540) // the ninja attacks
{
channelNinja.setAnim("Attack1");
n = 0;
}
else // n < 540
{
channelNinja.setAnim("Idle1");
}
}
[/java]
Then the ninja's attack animation does not show. The only animation shown would be idle1.
You set it to idle every frame…so anything else is getting overwritten before it shows.
Just look at your comment next to the else and you can see that its totally different logic to what an else actually does…