Unity - How to Smooth Out Constant Velocity on Rigidbody2D - unity3d

I am setting constant velocity for my character movement in my 2D game. However since using the method my character seems to be shaking a little. Is there a way to fix this and smooth the movement out?
Here is I set the velocity in the Update function:
constantVelocity = new Vector3 (playerInputX * speed, playerInputY * speed, 0);
I then apply this velocity to the the Rigidbody2D component in the FixedUpdate function.

It turns out that my problem had nothing to do with the object I was moving but the camera that was following it. The camera was trying to Lerp towards the object in Update. I changed this to FixedUpdate and it now works perfectly.
Thanks for the help anyway,
Tommy

There are two ways to do this.
You can always use Time.fixedDeltaTime to smoothen your player movement.
You can try low pass filtering on our constantVelocity.

Related

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.

Make Physics more accurate

I make a 2D-game. In this game, I have a projectile, which I move the fires by myself like this:
void Update()
{
gameObject.transform.position = new Vector3(
gameObject.transform.position.x + baseVelocity * Time.deltaTime,
gameObject.transform.position.y + baseVelocity * Time.deltaTime,
gameObject.transform.position.z);
}
And also, I use void OnTriggerEnter2D(Collider2D other) to know when collisions occurs.
The problem is when game running on a weak phone with 30-FPS it won't detect collision, while same fire in an 60-FPS phone will collide.
I think this is because the fires move 2x more in 30-FPS phones.
One option was use FixedUpdate() method for moving fiers, but it gave me jerky movement, and I used Update() method for moving because it gives me smooth movement(in both of them I used Time.deltaTime).
Can you please suggest me how make more accurate while using smooth movement?
like move object in Update() method but check object position in more(offset) positions than where it is!
I forget to note that I changed the value of Fixed TimeStep to 0.01 for getting more accurate physics.
Thanks in advance.
edit:
I finally end up with using FixedUpdate() for move objects by myself, for so many reasons I can't use Physics engine, I set FixedUpdate TPS(tick per second) around 60 to some how be match with my 60 FPS Update (and still can't figure out why increase FixedUpdate TPS will make object movement jerky!).
And will keep this question open for finding better answer.
You seem to be doing the physics manually. The issue with manually updating the position is that you don't know about the position of your projectile between frames. Even if you were to use FixedUpdate, you could still be in the situation where two FixedUpdate calls cause your projectile to miss an object.
So instead of manually updating the position, you should use a RigidBody2D, with the collision detection set to "Continuous". A continuous collision detection will interpolate object collision in between physics update cycles, so you hit your target even at very high speeds. Also, this solution does not depend on frame rate.
Note that the above will only work if you don't manually update your positions yourself. That is because the physics engine uses velocity to calculate collision in between physics update cycles.
So instead of updating the position yourself (which is bad for physics), use forces instead.
Or alternatively, if you don't want to mess with forces, update the velocity instead:
GetComponent<RigidBody2D>().velocity = baseVelocity;

UNITY3d The Camera Follow and Background move are not smooth

I just learn Unity3d for a while, then I have a question about The Camera Follow and Background move are not smooth. I create a cube with ragidbody2d , then i use two dufferent ways to make camera follow :
a. I make the camera in the cube
b. I use Script to control the camera (lateupdate use)
And I add some pictures which are converted into sprite (the size is 1024*512). I make them to backgound.
when play it , both methods have the same problem, the background move is not smooth, but not completely smooth at all,it will appear from time to time.
I tried the official 2d example found in fast-moving role the backgound is not smooth too. But there are some game made by Unity like badland, when I play it ,there is no this problem.I dont know how to solve this problem, Is the camera configured ? Scirpt? The Type of texture? compressed way? or Need to use some plug-ins?
please help me .thank you .
use "LateUpdate"
LateUpdate is called after all Update functions have been called. This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
It probably because the updates you're using are not synced.
There's the main update (triggered each frame, time between those are different (Time.deltaTime)), FixedUpdate (triggered every physics update, which is a fixed time (Time.fixedTime)) and LateUpdate (every frame, but at the very last).
If you're trying to follow a rigidbody affected by physics, it's recommended to set the camera update function to Late- or FixedUpdate.
You can use it in the Update function if you set the interpolation of the rigidbody in it's inspector:
A rigidody gets an update for it's position every x fixed time, the Update function can be triggered 5 times when FixedUpdate has only been called once, or vice versa. This will give strange results, so setting the rigidbodies interpolation will smooth things out between those unsynced updates.
Also, you can move the camera smoothly by using Lerp methods. an example:
void Update() {
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, targetPosition, Time.deltaTime * 5);
}
more about lerping here
Hope this helps
I found another problem that I don't move camera at all, just move a GameObject which has some pictures as backgound. The Script of Moving GameObject is
Update()
this.transform.Translate(Vector3.right * Time.deltaTime * 40.0f, Space.World);
or
Vector3 pos = this.transform.position;
pos.x += (100.0f * Time.deltaTime);
transform.position = Vector3.Lerp(transform.position, pos, Time.deltaTime * 50);
the reuslt is the background move is not smooth with a visual lag, but not completely smooth at all,it will appear from time to time.
Is there some configures wrong? Graphics Emulation? or something else ?

Rigidbody Velocity Messing Up Physics UNITY

I am trying to move my player by using rigidbody.velocity:
rigidbod.velocity = new Vector2 (Input.GetAxis ("Horizontal") * maxSpeed, rigidbod.velocity.y);
the problem is, this messes up of my explosion code. The character is supposed to be knocked back when near an explosion. I know why it happens; if the player is still, the rigidbody's X velocity would be returned as 0, meaning any outside forces pushing the player along the X axis would counteract this. So when I add the explosion, the player cuts to his new position a few units away. It looks very unnatural and jerky, as he should be pushed back, but his code is telling him to be still unless a key is pressed. I'm posting this to see if there's any way I can re-write this code to be able to move the player while being pushed correctly from outside forces. I heard that AddForce works, but when I used it, my player's velocity constantly increased. He is wither way too fast or way too slow. Any ideas on how I can get this to work? I tried adding rigidbody.velocity.x after where it says 'maxspeed' hoping that it would allow outside force input, and it works, but it messes up the movement code, making him go way too fast. I can't seem to get both the explosions and the movement code to work correctly at the same time. Any help would be greatly appreciated. Thanks.
which is exactly why in the Unity docs they explicitly state:
In most cases you should not modify the velocity directly, as this can
result in unrealistic behaviour.
instead of modifying the velocity directly, you should be using an AddForce(..)
Vector2 force = new Vector2 (Input.GetAxis ("Horizontal") * maxSpeed, 0f);
rigidbody.AddForce(force);
//or if in update:
rigidbody.AddForce(force * Time.deltaTime);

Making an Object faster in unity3d

I'm making a 2D game in unity and I'm stuck. I have a rigidbody2d projectile that is fired based on the click position. I want to make this projectile faster without changing its trajectory or changing the timescale of the entire world and I cant change the gravity scale. Any ideas ? Thank you in advance :)
give the object less mass, for one. But if you are using a Vector for velocity so.
Where ever you are setting the velocity of the bullet in the script, add this (in JavaScript)
var speed = 2.5; // or what ever you want it to be, play with it
// as long as you multiply both x and y the same, they will keep their direction.
gameObject.rigidbody2D.velocity.x *= speed;
gameObject.rigidbody2D.velocity.y *= speed;