Unity 2D - Freeze axis and AddForceAtPosition - c#

I'm learning Unity 2D and I want to do something like this:
Basically the character runs into the box/edge and it falls over. BUT! I want to lock down the X axis somehow. So in reality it would kinda look like if it was just rotated simple by 90 degrees (with some kind of acceleration).
I've tried to do it with rigidbody2d and edgecollider and AddForceAtPosition, but I failed miserably.
What I really wanted to do is lock down the "wall" and apply the force at the very top of the rigidbody so it would just fall over to the right, but it simply didn't work out.
Any help would be appreciated!

Remember that same logic applies to physics in games as it does to physics in real life. Just make a hinge and put the anchor and connected anchor at the same location at the bottom and set a limit for hinge.
Wall:
Hinge:
I set Lower Angle to something near 90, otherwise wall becomes uneven when it drops.
Remember to put a Rigidbody2D and a Box Collider for wall.

Firstly, I would lock the rotational axes that you don't want to move on your rigidbody, then, if your object origin is located on the ground / bottom of the object, you can add rotational torque to the object to achieve the affect you want :) http://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html

Related

How to simulate running on Wall like The Flash in unity

Please is it possible to make a Player with rigidbody or character controller run on wall just like The Flash. Please guide me on how to implement this.Here's a link of what I'm talking about. Thank you in advance.
Rotate the player to be vertical to the building, adjust the gravity parameter on your rigid body (make it lower so the added force won't be canceled by it) and just add the force (or set the velocity, whatever makes you happy)
I'm going to assume you already know how to move the player around on the ground. All you need to do is create some form of detection for when they want to begin running on the wall (maybe the player presses a button, or walks up to the wall), at which point you will want to do 2 things: rotate the player to have their feet on the wall, and change gravity to be pulling the player into the wall.
If we assume the player is facing the wall when they go up on it, you can just rotate 90 degrees backwards. Changing gravity should be easy too. Once the player is fully rotated, do something like this
Vector3 newGravityVector = tranform.down.normalized
Physics.gravity = transform.down.normalized * 9.8 //9.8 is the default gravitaty value, feel free to use a different multiplier if you wish.
(this script should be attached to your player)

How to handle multiple rocket thrusters?

I'm fairly new to game development and I'm trying to develop a 2D game in unity where the main character has a jetpack with two thrusters and I want him to control each one individually. So if he only turns on one thruster he goes 5 meters above ground and hovers there while using both would make him hover at a height of 10 meters. How would i go about doing this?
I tried just simply adding forces as if the character was jumping and freezing the y-axis to 0 until he let go of the jetpack button but that did not give me the feel i was looking for. I also tried raycasting a line out of the jetpacks to the closest surface and adding a force to the jetpack to allow the player to float at that height but that I couldn't figure out a proper way to implement it.
The Raycast sounds like a great idea. Do the raycast to the next available platform and do as #Ahndwoo said in a comment, `
What you would need to do is divide the force of your thrusters by the
magnitude of the Ray. Thus, as the Ray gets longer, the force
decreases.
By doing this you'll get the natural movement from a force and you'll control how high can you get.
I would Make your thrusters holder as an object and as more turbines it has, the more powerful i'll get.

How do I fix the floating glitch that is happening to my Player Ball?

I am using Unity3D to make a replica of the game "Rolling Sky" which you can find on the Google/Apple app store. I was able to make a simple floor which the ball will move on and I was also able to make the ball (Player) move left and right. After moving the ball a couple of times back and fourth, it starts float in the air and eventually what it seems like is change it's axis.
I came up with a couple of alternatives that I think would work, but I had trouble coming up with the code to make it function properly.
1) Do not rotate the ball at all. Just have it smoothly move back and forth.
^^ Probably the best solution I could come up with.
or
2) Use CharacterController to have complete control of how the ball reacts to different events, scenarios, etc.
^^ This would probably be the most necessary and hardest of I had to guess.
or
3) Move the ball an x amount of pixels everytime I move left or right.
^^ I would assume this would create a lot more glitches than what I have right now.
public class PlayerController : MonoBehaviour
{
public float speed;
void Update()
{
transform.Translate(Vector3.right * speed * Input.GetAxis("Horizontal") * Time.deltaTime);
}
}
EDIT:
I was able to provide my own answer, but if anyone has any alternatives, feel free to post your solution!
To prevent the player ball from floating into the sky, no other code was needed. Simply go to the Rigidbody component on the Player and expand the "Constraints" menu. Next, check the axis boxes that you would like to freeze the rotation on to prevent the ball from spinning like so in the picture.
Click the link to see the picture
here.
In this case, I did not freeze the ball on the x axis because I eventually want to animate it to spin as if it is rolling forward.
You're using transform.Translate
and
You're using Vector3.right
transform.Translate will directly move the ball without thought of rotation. You will need to take the object's rotation into consideration when translating. You can also use the physics engine and use Rigidbody.MovePosition to force position or Rigidbody.AddForce to utilize physics interactions.
Vector3.right will use the constant value equivalent to Vector3(1, 0, 0). This will not be in regard to the object's rotation or facing.
I suggest looking through Unity's tutorial for a ball-roller and see how they complete the task of user input with a user controlled ball.

Weird behavior when rotating 2D GameObject with hinged children in Unity

For more than two days now I am struggling with this seemingly impossible problem: I have a composite GameObject (one parent, 10 children), each one hinged to at least another with angle limits and player-controlled motors. Moves perfectly as I wish.
Now I want to be able to flip it. After a while and many trials,rotation seems the best way in order to keep the angles (inverting localSpace does not respect them):
Vector3 rotPoint = new Vector3 (ParentGameObject.transform.position.x, myY, myZ);
ParentGameObject.transform.rotateAround (rotPoint, Vector3.up, 180.0f);
BUT, if the parent rotates as asked (180 degrees around its Y axis), every child flips but not around the same axis, rather around their own rotation center. This gives ridiculous results as hinges try to get back to the desired position. How could I fix this?
Many many thanks in advance...
In the end I figured out the problem. My problem had no valuable solution.
We can rotate the transform around the Y axis even in 2D, but NOT the rigidbody2D, which in the same object will invariably cause serious problems.
This is maybe one remaining inconsistency into Unity5's implementation of 2D.
I had to flip using localScale inversion and programmatically updated positions and rotations.
It sounds like the child GameObjects are rotating around their own pivots instead of the parents. I haven't got Unity installed at the moment to test this, but if you add an empty GameObject as a child of your parent (and with the same pivot point), then make all your other GameObjects a child of this, it should work.
You should not move or rotate a rigidbody if you don't have to. That will mess up the underlying physics calculations and physics engines won't be happy about it. Instead, you have other ways to interact with them
Apply force, torque
Apply acceleration, angular acceleration
Change speed, angular speed
If you have to move/rotate the object, you should also update positions, rotations, speeds, angular speeds of all of your hinged objects. That would not be easy. Give some more details if you unable to solve.

Rotate an object according to a plane normal

I am making a FPS game, I created a peaceful AI for the moment and when the character is died, I just want it to be oriented according to the normal below it. I show you the result for the moment :
as you can see, the character is flying, because the terrain is not straight.
I am trying (without success) to make something like that :
I have the (x,y,z) coordinates (character position) and the normal to the plane.
Yes the normal is always facing up, as you can see on my drawing, even if I drew it in the good pose. I dont understand when you are talking about the quaternion, which new normal are you talking about ?
I already have the normal of the plane under the character, so, part of the jobs is already done :)
Your character's death pose is always with the normal facing up, right?
And you want to rotate it to another normal.
You can find a Quaternion that represents the rotation between two vectors (up and the new normal) to rotate the mesh.
This question has the answer to that:
Quaternion from two vector pairs
To know the normal of your terrain, you can probably cast a ray down and get the normal from the collision information depending on the physics engine you are using.
Edit:
Some background info:
A quaternion represents a rotation. So what I'm suggesting is that you use the answer from the question I linked to calculate the rotation between the default orientation of the character's death pose (UP) to the new orientation (Terrain Normal)
Then you can just transform your dead character with the Quaternion and it'll follow the terrain's normal.
Here's a sketch: calculate Q from UP and N with the solution from the Link and rotate your character's model with Q:

Categories

Resources