2D parallax with gyroscope/accelerometer avoiding z tilt - accelerometer

We are building an Unity game as a school project and we need to produce a parallax effect for different layers of gameObjects.
layer.transform.position = new Vector3(
center - (Input.gyro.gravity.x * layerDistance)),
layer.transform.position.y,
layer.transform.position.z
) ;
Using both gyroscope X or accelerometer X this works quite right.
However we need to Z tilt in order to change the world's gravity. Therefor X tilt is influenced by Z tilt, so when we change the gravity (by rotating the device), the parallax is quite affected and mustn't change.
Would there be an algorithm or a sample code to solve this Z tilt effect ?

Related

How to change the direction of gravity in Sprite Kit

I'm trying to build a game about billiards. The screen is like a table, the default direction of the gravity system is downward, that is, the Y axis. And I want to change this direction to Z axis, so the ball has both gravity effect and will not fall down. Does anyone know how to do this? Thanks.
physicsWorld.gravity in SpriteKit is a 2D vector (dx,dy) and thus has no concept of the z axis.
for a top-down billiard game you might try turning gravity off self.physicsWorld.gravity = .zero and then use velocity and linearDamping on your billiard ball nodes to simulate realistic rolling.

Blend Trees are bad in 2d

My project is Isometric 2d with 8 directional movement animations.
As you all know, blend trees are great for blending animation in 3d. It also works in 2d (visual aspect) but "under the hood" I have a lot of issues.
I like to bind Animation Events to different functions. But they trigger more than once, because 2 or 3 animations are being played at the same time in my blend tree (although only the dominant one displayed) if I move in a not straight direction. In other words, if my x and y axis are not equal to 1 or -1.
I found a workaround that checks which animations weight is >= 0.5f, but that doesn't work in all circumstances and i don't know why and I would actually like to know whether there are better ways to workaround this issue.
This issue is present only on the Enemy character, because the x and y axies are not always equal to a whole number, as it modifies x and y based on a direction to the player. The player, on the other hand, works fine with the keyboard, but does not with a joystick due to the same reason.
Could I somehow "clamp" the value to be a whole number of the vector.x and vector.y to be strictly 1 or -1 in this situation? That would help a lot with Animation Events and with that blend tree situation.
For now its just simply doing this:
direction = (target.position - transform.position).normalized;
anim.SetFloat("Horizontal", direction.x);
anim.SetFloat("Vertical", direction.y);
Then I tried this but it doesn't work:
direction = (target.position - transform.position).normalized;
anim.SetFloat("Horizontal", Mathf.Clamp(direction.x, -1, 1));
anim.SetFloat("Vertical", Mathf.Clamp(direction.y, -1, 1));

cylinder simulation on top of each other in unreal engine 4

I created 2 cylinders and put one on top of the other. Then I clicked to simulate physics and on each cylinder I added the following image blueprint, added a 50-point rotation on the z axis of each cylinder in opposite directions.
It turns out that in the simulation, when I perform, the cylinders rotate in one direction and move on the ground in the other direction. If it turns clockwise it moves left, and vice versa, and should be the other way around.
Can anyone help me solve this? It's for both cylinders to work together and I see how their simulation is accelerating with a constant rotation, but that's not what happens
If you want to simulate physics you should be applying forces to the cylinders using Add Torque in Radians or Add Torque in Degrees rather than modifying the rotation directly.
Alternatively, if you want to precisely control the cylinders, do not simulate physics. Instead, disable Simulate Physics and animate the rotation and position of the cylinders directly as you are.

Projectile Motion in Unity

So my friend and I are making some 2D game, we are using some custom character controller, so we are not using rigidbody2D. Now we have some sort of catapult which needs to eject the player in a projectile-motion style.
We've done it for the catapult which shoots the player straight up
In inspector you can decide how much units do you want player to jump and how much does it need to get to reach max height.
So here is the code for the catapult that shoots the player up.
float ejectInicialY = (jumpHeight - ( player.physics.gravity * Mathf.Pow(timeToReachMaxHeight, 2) / 2)) / timeToReachMaxHeight;
float ejectVelocityY = ejectInicialY + player.physics.gravity * Time.deltaTime;
player.physics.playerVelocity = new Vector2(ejectVelocityY, 0f);
I tried to apply the same formulas for the X coordinate, but it doesn't work well.
Any help would be greatly appreciated.
This is ultimately a physics problem.
You are calculating current velocities by determining the acceleration of the object. Acceleration of an object can be determined from the net force acting on the object (F) and the mass of the object (m) through the formula a = F / m. I highly recommend reading some explanations of projectile motion and understanding the meaning of the motion equations you are using.
Vertical Direction
For the vertical direction, the net vertical force during the jump (assuming no air drag, etc.) is player.physics.gravity. So you apply your motion formulas assuming a constant acceleration of player.physics.gravity, which you've seemed to have accomplished already.
Horizontal Direction
Becausegravity does not commonly act in the horizontal direction, the net horizontal force during the jump (assuming no air drag, etc.) is 0. So again you can apply your motion formulas, but this time using 0 as your acceleration. By doing this, you should realize that velocityX does not change (in the absence of net horizontal force). Therefore the X coordinate can be determined through (in pseudo-code) newPositionX = startPositionX + Time.deltaTime * velocityX

How to change gravity direction on Sprite Kit?

I am using Sprite Kit in Xcode and I was wondering how to change gravity direction.As default gravity direction to "X" you can imagine on below axes graphic.What about if I would like to change to "Y".
My goal is giving to object the falling effect.Its like falling from hight point and touching the ground than getting respond with physics!
(Could be dices on board game)
//Default gravity direction is X
SKSpriteNode *myNode =[SKSpriteNode spriteNodeWithImageNamed:#"ball"];
myNode.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:self.frame.size.width/2];
[self addChild: myNode];
Thanks in advance!
You can apply a vector to the Physics World of your scene using this code
self.physicsWorld.gravity=CGVectorMake(0,-10);
In SpriteKit, X and Y are the default coordinates that you see on the screen, and the Z coordinate is the order in which the objects are positioned (the zPosition). Since SpriteKit uses a 2D game engine, you do not have a third dimension, Z, to utilize. You can change the gravity between Y and X (Top/Bottom and Left/Right of screen respectively), but not between the Z coordinate. If you want to recreate a "dice falling" effect, I would recommend you create a Sprite called Dice scaled to a large amount, and once you add it to the scene you scale it down in x amounts of seconds.
[self runAction:[SKAction scaleBy:negativeFloatHere duration:3]];
This will make the dice appear to be falling, and you might want to add some spinning animations for it if you with. If you want to use the 3D engine, go try out Metal or SceneKit