flipping 2d object to face the direction its moving.
Just as the title says Im trying to make objects face in the direction they are moving. Here is the script so the objects travel left to right bouncing up and down when the range is reached it goes back to the left. I need the image to flip on its way back.
- //@input float distance = 1.0 {"widget": "slider", "min": 0, "max": 10.0, "step": 0.01}
- //@input float speed = 1.0 {"widget": "slider", "min": 0, "max": 10.0, "step": 0.01}
- //@input float range = 10.0 {"widget": "slider", "min": 0, "max": 80.0, "step": 0.01}
- var newX = Math.sin(getTime() * script.speed) * script.range;
- var newY = Math.sin(getTime() * 5) * script.bounce;
- var newZ = script.distance - 80;
- script.getSceneObject().getTransform().setLocalPosition(new vec3(newX, newY, newZ));
-
Hi John!
You can flip a sprite by enabling and disabling the flipX bool on the Component.SpriteVisual.
Here's a little script that shows this. Add this to your project and link it to a sprite in the Inspector. Then, in the inspector toggle the flipX checkbox and you should see your Sprite flipping back and forth in the Preview panel.
// -----JS CODE-----
// @input Component.SpriteVisual mySprite
// @input bool flipX
script.mySprite.flipX = script.flipX;Hope that helps!
Comment actions -
thanks! how would I trigger the flip when the object changes direction? My experience comes from Basic so im used to if then else. How does this scripting language handle it?
Comment actions -
You have access to the current position via getLocalPosition(). Let's say you get the current local position and save the Y, called currY. You also have access to the new position you are setting it (in your example, newY). Then, you can check if the newY is greater than the currY, then the object is moving up. If newY is less than the currY, then the object is moving down. And then will these conditionals, you can flip accordingly. The same principle will apply for X as well if you're checking moving left and right. Hope that helps!
Comment actions -
If you are facing any issue regarding 2D type lenses you canclick on this link to learn about it and try to clear those gaps from the lens.
Comment actions
Please sign in to leave a comment.
Have a comment?