2 of my 4 towers face the middle. The other two are doing their own thing!

:exclamation: :person_raising_hand: I have reproduced this on the playground! Tower Rotate Example | Babylon.js Playground (babylonjs.com)

:thinking: How to create an object rotated, then animated

I am getting started with Babylon.js and am getting stuck. I have this code to create boxes and add cylinders to them. When clicked, bullets fire out from the cylinder.

I’ve added a playground link here: https://playground.babylonjs.com/#ELP0A5#2

I’m placing the turrets on the field and attempting to rotate them all to face each other. With my code today, the first and final turret are place perfectly.

However, I cannot get the middle two turrets to face the right direction, maybe this image will help explain it.

I feel I need to flip the starting X for turret 1 and both the x and Y for turret two, but the syntax is alluding me! I wrote something similar in 2d using phaser and it was very simple, as easy as calling the FlipX function.

How do I create boxes 1 and 2 and have them also oscillate facing the center?

Effectively, they need to rotate the opposite way I’m guessing.

Don’t do the rotations on 3, add instead a turret based modifier to your oscillation keyframe values.

     var mod = i*Math.PI;
        if (i === 2) {
            var mod = .5*Math.PI;
        }
        if (i === 1) {
            var mod = -.5*Math.PI;
        }


        oscillationKeys.push({ frame: 0, value: 0+mod });
        oscillationKeys.push({ frame: 15, value: Math.PI / 2+mod });
        oscillationKeys.push({ frame: 30, value: 0+mod });
1 Like

You can use the relative loop mode and add a starting offset to the Y rotation of the turrets 1 and 2 (I also added the Math.PI offset to the X/Z rotations for turret 2, see line 108):

Note that this PG will work only when Animations: Fix loop relative mode to start at the current value of the animated object by Popov72 · Pull Request #14563 · BabylonJS/Babylon.js · GitHub is taken into account in the Playground (which was not the case yet when I tested).

I was stuck on this for hours! The rotation of the cube was never the problem at all, it was the direction of the animation loop!

Thank you very much :slightly_smiling_face:

@Evgeni_Popov, I am interested to see how your approach works when that Pull Request completes, I’ll check in a few days!

Working but not perfectly clean playground link here!

Tower Rotate, fixed | Babylon.js Playground (babylonjs.com)