Have fixed speed with AddForce? - unity3d

I want to use AddForce to move an object with a fixed speed but when I use AddForce the speed of the rigid body increases in crescendo.
AddForce adds a force to me every time I hold the input down, the more I hold the key to move the object the more force is added, while I always want to move the object with the same force.

You may set the velocity directly instead of adding accelerated force every frame you hold down the button. So instead of using AddForce(), you can access your rigidbody's velocity like in this example.

Related

Why is this Framerate Independent When Adding An ImpulseForce Unreal Engine

I am facing an issue I want to push the object in the direction/Axis the player is moving towards I can do that using the getlastinputvector multiplying the force value but the thing is it is framerate dependent the output velocity is different when testing it in like 10FPS or 1000(uncapped)FPS how can i achieve the force not being affected by framerate and also the force being applied towards the player moving axis.
According to the documentation of add impulse: https://docs.unrealengine.com/4.27/en-US/BlueprintAPI/Pawn/Components/CharacterMovement/AddImpulse/
"If you want to continually apply forces each frame, use AddForce()"
The add impulse is good for something like kicking a ball where it only happens once and the framerate would not matter.
The add force is better for something like having your character push an object as it is designed to be used every frame.
In your case it seems you should use add force.

Unity Player passsing through objects

player and object both have colliders and rigidbodies, object has position and rotation locked, player has only rotation locked. When the player goes to the blocks, the player goes through the blocks, although they do give a bit of resistance. To move the player im setting the rigidbody's velocity, and doing that in FixedUpdate.
i have no idea why this is happening, any ideas?
main part of the code is:
rigidBody.velocity = new Vector3(direction.x, rigidBody.velocity.y + (-Gravity * Time.deltaTime), direction.z);
(direction is determined by the WASD keys, and i'm using my own gravity)
First of all, you do not need to multiply the velocity by time.DeltaTime, because you are moving your object in the FixedUpdate() method; Which uses fixed time intervals since the physics engine does not run in sync with the regular game engine. Also, both objects do not need rigidbodies in order to collide with one another. I suggest looking at your collision matrix in settings and verifying that everything you need collision for is checked correctly. As others have said as well, check your kinematics on the rigidbody.
A last suggestion for working with your own gravity. Do not change the actual gravity value of the game engine. It is typically recommended that you use a multiplier variable and apply it to the constant gravity already set by the physics engine. If you are completely editing the gravity, than maybe consider using a character controller instead.
I guess it has something to do with what the documentation says "In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour".
Try to use AddForce() or similar functions to alter the properties of the rigid body. Colliders etc will then work as expected.

I am developing a game at unity and encountered a problem while using accelerometer

I have written this code for adding a force on a 3d box using accelerometer but this has no effect on the box when running. Is there any way of using AddForce instead of transform?
Also I only want to add a force in the x-axis, means horizontally, and in no other direction. Is there any way to do this?
You might need to check your rigidbody mass with AddForce(). Force by default works with Newton's Law, that force = mass x acceleration. You might want to play with the ForceMode optional parameter that can be passed like: rb.AddForce(tilt, ForceMode.Acceleration). Acceleration adds a continuous acceleration to the rigidbody, ignoring its mass. More information on: https://docs.unity3d.com/ScriptReference/ForceMode.html
Or you might want to check rb.MoveToPosition(Vector3) that calculates force for you to move to specified position.
https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
Information on the default way AddForce works, Rigidbody2D works in a similar way minus the third dimension.
https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html
And to restrict force to only the x axis you might want to look on constrains on your box, limiting by axis or rotation.

Changing the speed at which an object drops when affected by gravity.(SpriteKit)

I have an object that needs to fall down the scene when the game starts. When the player taps the screen the object moves back to the top of the scene. In order to make the object fall I set it to be affected by gravity. This works fine but the problem is the object falls down the screen to fast. Can I set the speed at which the object falls do to gravity to a different speed. If so how do I do this.
You can reduce the gravity of the physicsWorld. Another option is to apply a force in the opposite direction to each object. If the gravity is -9.8 in the y axis, you could apply a force from +0-9.8 in the y axis to slow the object down.
EDIT: Changing the mass does not affect the speed.

How to use AddForce and Velocity together?

I have the jump component who use the AddForce for jumping and the movement component who move left and right using the Velocity.
If you don't move the character when your are jumping the jumping will be fine but when you move the character and jump at the same time then the movement component will break the jumping because the velocity is setting up a Vector2 point where define the Y axis too. I tried to use the current Y axis from the transform component in the movement but even that doesn't work.
What I should do for fix the problem between AddForce and then use Velocity?
It seems your move function is creating a new velocity vector and overwriting the existing one.
Vector2 velocityVector = rigidbody.velocity;
velocityVector.x += movement * force;
rigidbody.velocity = velocityVector;
This will retain the existing velocity, both X and Y, and modify it. You will of course need to add deceleration (usually I use something along the lines of if(grounded) velocityVector.x *= 0.999f;, but I'm sure more fancy maths exists for more realistic deceleration) and some kind of maximum speed (again, I keep things simple and use similar to if(velocityVector.x > maxSpeed) velocityVector.x = maxSpeed;).
Rigidbody.AddForce has the following definition:
public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force);
One of the options available for ForceMode is ForceMode.VelocityChange:
Add an instant velocity change to the rigidbody, ignoring its mass.
Apply the velocity change instantly with a single function call. In contrast to ForceMode.Impulse, VelocityChange will change the velocity of every rigidbody the same way regardless of differences in mass. This mode is useful for something like a fleet of differently-sized space ships that you want to control without accounting for differences in mass. In this mode, the unit of the force parameter is applied to the rigidbody as distance/time.