I'm making a 2D game where the player is able to jump only when the
Y velocity==0, the player moves only on the X axis so I don't see a reason for the Y velocity to either decrease or increase. The problem is that it's weirdly changing.
Please help me fix that RigidBody2D velocity's problem, it's making me crazy.
Thanks.
I'm showing you how the velocity is changingThe Colliders of the objects that are causing this problem
edit: I restarted unity but it didn't work, even when I'm not moving the Y velocity is increasing.
You should'nt compare float numbers because they are not fully precise.
Check IEEE 754 and instead of comparise them, check if it's greater than some value. Also look at you're y value in the rigidbody component to be sure.
There are two solutions:
Check if the speed y is very small, as they suggested.
Use Freeze Position Y. By activating that box and deactivating it
when you have to move vertically, for example to jump, you will be
constrained to the Y axis, and therefore the speed will not change.
Good work!
Related
I have several objects with Rigidbody2d that I stack on top of each other. As soon as there are a few more, they get presst in each other and I just can't understand why. can someone tell me why this is happening?
Rigidbody2d
dynamic
Mass between 1 and 20 depends on Object
Collision detection: Continuous
Interpolate: Interoplate
i use Box and Polygon Collider.
Global physic settings i changed Max Linear Correction to 0.0001 because
when this value is higher the objects begin to jitter.
This is what happened:
see here a .gif who show what happened
please help.
I am working on an Asteroids clone in 3D with top-down camera. This setup is static and will not change (so no, I do not want to transform the project to 2D game instead).
This means that I need to limit all movement to X and Y axis. I created movement for both asteroids and player and everything worked fine. All movements are done using AddForce on respective RigidBody components.
The problem is then I start dealing with collisions. I use Mesh Collider components to get a nice and precise "touch reaction". The problem is that when collision like this occurs, the new movement vector has Z value different from 0. This is a problem as the object will start moving on Z axis.
What have I tried:
Freezing constraints on RigidBody
Manully reseting Z in Update function
The first solution (freezing constraints) did not work and neither did the second one (moreover, the second one seems quite messy)
So the question is
What would be the optimal way to force physics-based movement only to X and Y axis while using precise collision with Mesh Colliders?
are you sure you used the position restriction correctly? You can check to set the restrictions with a vector as in the documentation. https://docs.unity3d.com/ScriptReference/RigidbodyConstraints.FreezePosition.html
to see how its done. If not please share the code or a screenshot of the rigidbody restrictions you tried in the editor
What is the ideal way to implement projectile physics, like a cannonball or arrow? I've been experimenting with BodyForce / BodyVelocity but those look like they're applying a constant, non-diminishing force / velocity to the projectile which doesn't look realistic. I've also just set the Velocity property of the projectile directly which works better, but I'm assuming there's a standard pattern used for something like this.
In physics, velocity is split into 2 components, the vertical component and the horizontal component. When a projectile is in motion through the air, we don't consider air resistence affecting its speed, as it's such a minute force.
Since velocity is split into 2 components, if velocity changes or is applied horizontally, that horizonal velocity or force does not affect the vertical force or velocity, and vice versa. This is why projectiles travel at a constant speed horizontally. The vertical velocity, however, is not constant. Due to gravity, the projectile will fall to the earth faster over time. This is acceleration.
What you could try is to add a body velocity with a high value, such as 400,400,0: a velocity for the x, y and z axis, and then immediately remove the velocity a fraction of a second later. What this does is creates an initial velocity, which eventually gets changed by gravity on the y component.
To learn more about projectile physics, you can check out online resources such as the following:
http://bowlesphysics.com/images/AP_Physics_B_-_Projectile_Motion.pdf
I made a very simple program where it creates edgecolliders on the edges of the screen, and then launches an ellipse that has a circleCollider2d attached to it with impulse force.
I set all the colliders (edge and circle) to use the bouncy material with bounciness set at 1.0 (infinite bounce).
I am having a strange issue where over time, one of the axis of velocity converges to 0. For example
this.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (100, 100), ForceMode2D.Impulse);
What happens is that the object will start moving in a diagonal fashion, but given enough bounces, the velocity goes to just alternating between (100,0) and (-100,0). This can happen either where the x axis goes to 0 or the Y axis goes to 0. It depends on which one was higher during the start.
I am not sure why this is happening. Basically visually, the object will start bouncing around, and after about 10 bounces or so, it will just be bouncing back and forth in a straight line instead of preserving the other axis as well. I have no idea why it does that. I don't have any of the constraints on (I also tried constraining Z axis rotation, but that did nothing).
bouncyness on all edge colliders as well as the circle collider are set to 1.0.
The rigid body has mass at 1, and all drag coefficients are set to 0 as well.
The intention is for it to just bounce endlessly.
I will answer my own question here as I just realized why this is happening once I observed the behavior of the rotation. It completely did not occur to me that friction would have a hand in this, especially since friction in this case was resulting in the same simulation each time.
Along with turning bounciness to a 1, you must also turn friction to 0.
at the moment i'm working on a 2D plattformer and the work is going very well. But there is one Problem i can't get rid of.
The player can use a dash, which should move the player very fast in the direction he is looking. My problem is, that the player game object instant appears at the target location... like a teleport.
I'm using the AddForce Function to move the game object. On jumping i'm using AddForce as well and there it works really nice, i get a smooth jump movement.
The only different between dash and jump is that on jump i apply force to the y axis and on dash to the x axis. Changing the amount of force doesn't affect the movement only the distance.
Does anyone have an idea what i'm doing wrong?
// Dash
rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
// Jump
rigidbody2D.AddForce (new Vector2(0, jumpForce));
Best,
Verdemis :)
EDIT: I can not access my project at the moment, but i will try to show you what i have done.
Note: dashSpeed is a float value, at the moment something like 3500
and direction contains 1 or -1, depending on the direction the player is looking. The Dash code is part of the Update method.
// Dash
if(Input.GetKeyDown(dashKey))
rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
What is your direction vector, is it normalized? Since multiplying non-normalized vectors can be rather hazardous. Do you have more of the script to show?
EDIT: You should always do physics things in the FixedUpdate loop(link).
Ok i could solved the problem. My problem was that i only did this AddForce only once. The AddForce code was only executed in a single frame. I added a time which i count down to define how long the dash movement gonna be!
The problem may be that you are using a very big force, I was messing around with some physics today and realized that even a force of 100 almost looks instant. Try making a smaller force number to see if that helps you. I just tested making a smaller number and that does not work.