Is there a way to pause/play the parallax background in flame-engine? - flame

In my game, I have a parallax background which animates when the player is moving. I want the parallax to stop when the player is not moving. Is it possible?

The ParallaxComponent from Flame just wraps a Parallax object that controls everything. This class has a baseVelocity property that should always be set to the velocity of the player (in case the player can change velocities arbitrarily). If the player can only either move (with same speed) or stop, you can just set it to 0 or not 0.
final c = ParallaxComponent();
// you can do this on your `update` method setting it to `player.velocity`
c.parallax?.baseVelocity = Vector2.zero();
// if `parallax` is null it just means it has not been loaded yet

Related

"Attaching" instantiated prefab to a gameobject

I have 2 game objects (2D project):
GameObject enemy1 = GameObject.Find("Enemy1"); // Sprite Renderer "Order in Layer" 1
GameObject enemy2 = GameObject.Find("Enemy2"); // Sprite Renderer "Order in Layer" 2
A fire prefab is instantiated (just a fire animation):
GameObject go = Instantiate(Resources.Load<GameObject>("Animations/Fire1")); // Sprite Renderer "Order in Layer" 5
go.transform.SetParent(enemy1.transform);
go.transform.position = enemy1.transform.position;
Since the fire prefab's Sprite Renderer's Order in Layer is 5, it always on top of both enemies.
I would like the fire to appear above enemy1 but behind enemy2 regardless of what their Order in Layer is changed to. Basically, it should look like the enemy is catching fire, even if it moves or if it's layer order changes.
How can this be achieved? I thought making the fire prefab a child of the enemy gameobject would do it, but it doesn't.
Edit:
Making the fire animation a child of the enemy manually in the editor works perfectly. How do I replicate that with code?
Instantiating the prefab in this way fixed the issue:
go = Instantiate(Resources.Load<GameObject>("Animations/Fire1"), enemy1.transform);
Changing the added prefab's sortingOrder was also necessary to make things working correctly:
go.GetComponent<SpriteRenderer>().sortingOrder = enemy1.GetComponent<SpriteRenderer>().sortingOrder;
After this there was a small issue - the instantiated prefab would randomly appear behind or in front of the enemy. This was fixed by adding a Sorting Group component to the fire prefab.

vuforia-ball keeps falling down although it's child of imageTarget and target is not detected

I'm trying to make a tilt maze. But as soon as I hit play I can see the ball position continously changing although the target has not been detected yet. Which results in no ball when the target is detected and the maze loads up on the imageTarget.
If I check is Kinematic in the sphere(ball) rigidbody settings then the ball initiliazes with the model when target is detected but it stays at it's position until I uncheck is Kinematic and then the ball drops on the maze and moves as intented.
My sphere settings and maze floor settings are as follows
Ground properties
sphere properties
You can modify the DefaultTrackableEventHandler script as a workaround for that misbehavior.
There is the OnTrackingFound and OnTrackingLost events.
You can simply add something like this to the OnTrackingFound event to fix it:
MyBallScript ball = GetComponentInChildren <MyBallScript> ();
if (ball != null)
{
ball.rigidbody.isKinematic = true;
}
And do the same to reset the ball to any position you want in OnTrackingLost event, don't forget to make it kinematic again also.

Play two animations together on Spinner

I want to play moving and rotation animations on Spinner game object. For rotation I have used animator controller and for movement I have written code.
Here is animator controller inspector settings for rotation:
Here is the code that I have written for movement:
private void StartMoving()
{
iTween.MoveBy(gameObject, iTween.Hash("x", transformDistance, "time",
Random.Range(1.5f, 5f), "looptype", iTween.LoopType.pingPong, "easetype",
iTween.EaseType.linear, "delay", Random.Range(0f, 1f)));
}
At present only movement related action, I am showing in game play, rotation completely get stopped. If I set Apply Root Motion flag to false then rotation animation start playing and movement get stopped.
I want to play both animations together.
Here you have spinner object details:
Anytime you are combining an animation via the Animation controller and some other animation/motion via script it's best to separate those two objects (into parent child objects) as these two effects will conflict with each other.
Yes you could try some Apply root motion and other fancy stuff but it's always messy and I've found that separating them is cleaner and simpler.
In your case
Create a parent object and put your motion effect on that
Create a child object with your rotation object and animate it as you want.
Because the animation is now on the child object it wont be affected by the motion on parent.

Sprite disappearing to the back of another sprite on moving to defined point via transform.position

I have a difficulty with Unity2D's Sprite rendering.
Currently I have a sprite for a gameBoard, an empty GameObject holding the spawnPoint, a random sprite marking it, as well as a playerSprite to be instantiated as a prefab. If I am just using the hierarchy on Unity, the playerSprite shows perfectly above the gameBoard, and "hard-coding" its position will always keep it above the gameBoard sprite, visible to the eye.
The problem comes when I want to instantiate the gameBoard and dynamically adding the playerPrefabs into the game.
Here is the current code snippet I am currently using:
gameBoard.SetActive(true); //gameBoard is defined as a public gameObject with its element defined in Unity as the gameBoard sprite.
Player.playerSprite = (GameObject)Instantiate(Resources.Load("playerSprite"));
Player.playerSprite.transform.localPosition = spawnPoint.transform.localPosition;
The result is that the spritePrefab spawns at the place I want perfectly, but behind the gameBoard sprite, making it hidden when the game runs.
The result is the same when using transform.position instead of transform.localPosition
How should I code the transform part. such that I can still make my playerSprite visible? Thanks.
It's most likely not an issue with the position, but rather the Sorting Order of your Sprite Renderers.
The default values for any SpriteRenderer is Layer = Default & Sorting Order = 0
Sprite Renderers with a higher sorting order are rendered on top of those with a lower value.
Add the following lines to the end of your code, and try it out.
gameBoard.GetComponent<SpriteRenderer>().sortingOrder = 0;
Player.playerSprite.GetComponent<SpriteRenderer>().sortingOrder = 0;
Obviously, you could do the same thing in the inspector as well.

SpriteKit Physics Bodies Collisions in Multiple Layers

Okay, so I'm writing code for a space blaster game, and all seems to be going well, minus these crucial issues.
First, I added a spaceship (with PhysicsBody) as a child of self (GameScene).
Then I added a background layer as a node also as a child of self, behind the spaceship, and simulated movement by moving the background around behind the spaceship via joystick control.
I added a node with a boundary physics Edge Loop body so the background wouldn't move beyond the ship, and added that node as a child of the bgLayer.
I have objects for the spaceship to shoot at, but I want them to move around as the background does, so I added them as children of the bgLayer.
Of course, I needed a laser when the spaceship fires, so I added this as well as a child of the bgLayer.
_spaceship.physicsBody = (custom physicsBody code);
[self addChild:_spaceship];
_bgLayer = [SKNode node];
[self addChild:_bgLayer];
_bounds = [SKNode node];
_bounds.physicsBody = (physicsBody edgeLoop code);
[_bgLayer addChild:_bounds];
_otherObjects.physicsBody = (custom physicsBody code);
[_bgLayer addChild:_otherObjects];
_laser.physicsBody = (custom physicsBody code);
[_bgLayer addChild:_laser];
All is well, the background doesn't move beyond the spaceship,the other objects move as the background moves, and the laser fires when called upon.
My first big dilemma is when my laser fires, it does not move as the background moves, but the background moves behind it. I could probably make do but it looks funny if the laser always moves in parallel with the ship. This seems odd to me since I added the laser as a child of _bgLayer.
Second, my laser's physicsBody doesn't seem to recognize the EdgeLoop body, and sails right on through it. Also, my spaceship's physicsBody seems to recognize the EdgeLoop body, but it does not recognize the other objects that are children of _bgLayer.
Do Physics Bodies that are not children of the same layer recognize each other?
And why doesn't my laser behave similarly to other children of the same layer?
Moving the world by changing its position will affect
Children with physics bodies
Children without physics bodies
Moving the world by applying a force/impulse or by settings its velocity will affect
Children without physics bodies
Instead of moving the world by setting its velocity, you can add a camera (an SKNode with a physics body) to the world, move the camera by setting its velocity, and then update the position of the world to center the camera. You can center the camera in the didFinishUpdate method.
-(void)fireLaser
{
_laser = [SKSpriteNode spriteNodeWithImageNamed: [NSString stringWithFormat:#"blueLaser"]];;
_laser.zRotation = _spaceShip.zRotation;
CGVector rotationVector = RadiansToVector(_spaceShip.zRotation);
_laser.position = (Custom code for positioning the laser just in front of the spaceship);
_laser.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_laser.size];
[_bgLayer addChild:_laser];
_laser.physicsBody.velocity = CGVectorMake(rotationVector.dx * LASER_SPEED, rotationVector.dy * LASER_SPEED);
}
[self fireLaser] is called in touchesBegan when a particular SpriteNode is touched.
The laser fires beautifully, but does not scroll with the background, but rather moves in relation to the spaceship. Background scrolls with a PhysicsBody and a setVelocity method is called when the joystick moves, thus simulating spaceship motion, when in reality the spaceship never leaves the center of the screen. Physics categories prevent the background physics body from colliding with anything. LASER_SPEED is simply a static const float.