Make object move toward a position with a overshot effect? - unity3d

I'm trying to make an object moved to a position, but, depending on the speed it's going, I want it to overhoot it's target and bounce back to it.
This is how I'm moving the object toward the other now:
void FixedUpdate() {
transform.position = Vector2.Lerp (transform.position, blackhole.transform.position, Time.deltaTime * 25f);
}
This obviously doesn't account for the "overshoot" effect. However I've been having trouble to achieve it, since when trying with physics all my attempts end up with my object orbiting indefinitely around it's target, instead of passing it just once and moving inside it.

There are a few ways you could go about this. Nathan's suggestion of using "gravity" by applying forces every step I'll call the "Physics" solution. Your current solution that simply sets position I'll call the "Tweening" solution. Both can certainly work, and both can benefit from simply using if statements. For both solutions, stop the "orbiting" or head back from overShootPos with simple checks.
For Tweening:
When the move first starts, start tweening to a position past where you actually want to be.
Vector3 overShootPos = destinationPos + (destinationPos-transform.position)*overShootPercentage;
Then in FixedUpdate:
float deltaP = 0.001f;
if((transform.position-overShootPos).magnitude<deltaP){
//start tweening to destinationPos rather than overShootPos. Possibly just
overShootPos = destinationPos;
}
For Physics:
The forces actually will take the object past the destination, and it may start "orbiting" like you were saying.
In FixedUpdate, stop it once it's close and slow enough:
float deltaP = 1f; //may want larger delta here since physics is less precise.
if((transform.position-destinationPos).magnitude<deltaP && rb.velocity.magnitude<deltaP){
rb.velocity = Vector3.zero;
transform.position = destinationPos;
}
This is not all the code, but the basic idea should work.
Also, consider some of the differences between Physics and Tweening solutions (that may include code other than this). The Tweening will be very precise, and work basically the same way every time, where the Physics one may scale more crazily with large distances/fast speeds. The Physics solution is clearly needed if you do want the object to respond to explosions, collisions, etc.. on its route to the destination. If you go with Tweening, consider looking at tweening libraries like iTween and DOTween, especially if you'll be using a lot of tweens. They'll be faster (less Updates) and provide a nice syntax for doing stuff like this.

I would use a basic spring system. Equation of a spring
F = k(x-l) (Force = Spring Constant * (Extension - Rest Length)
So to calculate the spring force on your body:
Force = SpringConstant * (PointToHeadTo - CurrentPosition)
You then either use the physics damping in your engine or you apply your own damping
D = Velocity * -DampingConstant (DampingForce = Velocity * -DampingConstant)
Which you then apply as a force. However Unity / UE4 have damping co-efficients you set so you just need the spring force.
To keep things stable you can limit the Spring Force to a maximum and only have it take effect when within a radius of the target. You can also ramp up the damping as you cross the target to help bring it to rest.

Related

Unity: Calculate angular velocity of rigidbody to always face specific direction

I'm trying to make a pick up objects mechanic like the one in Amnesia. It's easy to calculate needed rigidbody's velocity, so that the held object stays in front of camera, but my problem is that the object doesn't rotate at all when I hold it. And I would rather have it always be rotated towards the camera. This could easily be achieved with simply parenting the object to player's camera, but...
The behaviour I'm after is as follows: if the bottle I picked up was standing on a table, with neck of the bottle facing ceiling, I would like to see this bottle always with its neck facing ceiling while I hold it. But if this bottle collides with something, it should behave like it actually bumped onto something, so it should rotate some small amount, but it should always try to return to its "original" rotation (in this case, neck facing ceiling).
I think that I need to calculate angular velocity for that and probably have some lerp to return to original rotation, but I'm at a loss on how to do that properly.
I think that the first thing I would need to do is to store the initial direction the moment player picks object up:
Vector3 targetDirection = playerCamera.transform.position - transform.position;
Script is on the held object, so "transform" refers to it. In FixedUpdate() I probably need to have some interpolation, so that angular velocity always tries to rotate the object to original rotation:
rigidbody.angularVelocity = Vector3.Lerp(rigidbody.angularVelocity, targetAngularVelocity, lerpSpeed * Time.fixedDeltaTime);
I don't know how to calculate targetAngularVelocity, because after all I would like the held object to return to original rotation smoothly. I'm not even sure if that's the right way to do this thing and perhaps I should do something else than to calculate angular velocity needed to rotate object properly. I tried just interpolating localRotation to original local rotation, but that did not allow the held object to bump on stuff (the movement then was very jittery). Any ideas?
You need a stabilizer. A script which will add torque/angular velocity to the object, whose angle is different from the target one. Say, you have two variables: targetDirection and currentDirection aka transform.forward. Then you write something like this in fixed update:
var rotation = Quaternion.FromToRotation(currentDirection, targetDirection).eulerAngles * sensitivity;
rigidbody.angularVelocity = rotation;
I recommend to set sensitivity about 0.05 and then increase it if the object stabilizes too slow.
Probably I confused the order, so you should put minus somewhere, but the approach itself is applicable.

Best way to move a game object in Unity 3D

I'm going through a few different Unity tutorials and the way a game object is moved around in each is a little different.
What are the pros/cons to each of these methods and which is preferred for a first person RPG?
// Here I use MovePosition function on the rigid body of this component
Rigidbody.MovePosition(m_Rigidbody.position + movement);
//Here I apply force to the rigid body and am able to choose force mode
Rigidbody.AddForce(15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
// Here I directly change a transforms position value, in this case the cam
Transform.transform.position = playerTransform.position + cameraOffset;
Thanks!!
EDIT;
Something I have noticed is that the applied force seems to memic wheeled vehicles while the position changes memic walking/running.
RigidBodies and Velocities/Physics
The only time, I personally have used the rigidbodys system was when implementing my own boids (flocking behaviour) as you need to calculate a few separate vectors and apply them all to the unit.
Rigidbody.MovePosition(m_Rigidbody.position + movement);
This calculates a movement vector towards a target for you using the physics system, so the object's velocity and movement can still be affected by drag, angular drag and so on.
This particular function is a wrapper around Rigidbody.AddForce I believe.
Pros :
Good if realistic physical reactions is something you are going for
Cons:
A bit unwieldy to use if all you are trying to achieve is moving a object from point A to point B.
Sometimes an errant setting set too high somewhere (for example: Mass > 10000000) can cause really screwy bugs in behaviour that can be quite a pain to pin down and mitigate.
Notes: Rigidbodies when colliding with another Rigidbody would bounce from each other depending on physics settings.
They are also affected by gravity. Basically they try to mimic real life objects but it can be sometimes difficult to tame the objects and make them do exactly what you want.
And Rigidbody.AddForce is basically the same as above except you calculate the vector yourself.
So for example to get a vector towards a target you would do
Vector3 target = target.position - myPosition;
Rigidbody.AddForce(target * 15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
If you don't plan on having any major physics mechanics in your game, I would suggest moving by interpolating the objects position.
As it is far easier to get things to behave how you want, unless of course you are going for physical realism!
Interpolating the units position
Pros :
Perhaps a little strange to understand at first but far simpler to make objects move how you want
Cons:
If you wanted realistic reactions to objects impacting you'd have to do a lot of the work yourself. But sometimes this is preferable to using a physics system then trying, as I've said earlier to tame it.
You would use the technique in say a Pokemon game, you don't stop in Pokemon and wait for ash to stop skidding or hit a wall and bounce uncontrollably backwards.
This particular function is setting the objects position like teleporting but you can also use this to move the character smoothly to a position. I suggest looking up 'tweens' for smoothly interpolating between variables.
//change the characters x by + 1 every tick,
Transform.transform.position.x += 1f;
Rigidbody.MovePosition(m_Rigidbody.position + movement);
From the docs:
If Rigidbody interpolation is enabled on the Rigidbody, calling Rigidbody.MovePosition results in a smooth transition between the two positions in any intermediate frames rendered. This should be used if you want to continuously move a rigidbody in each FixedUpdate.
https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
Rigidbody.AddForce(15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
This will make the object accelerate, so it won't travel at a constant velocity (this is because of Newton's second law, Force=mass*acceleration). Also if you have another force going in the opposite direction this force could get cancelled out and the object won't move at all.
Transform.transform.position = playerTransform.position + cameraOffset;
This will teleport the object. No smooth transition, no interaction with any forces already in the game, just an instant change in position.

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);

How to add torque to a rigidbody until it's angular velocity is 0?

I'm trying to figure out how to stop the rotation of a rigidbody using inertia in Unity 3D. This is how I assumed it should be done, but when I ran it, it crashed Unity and I had to kill the process.
while(rigidBody.angularVelocity.magnitude != 0) {
rigidBody.AddTorque(-transform.up * REACTION_WHEEL, ForceMode.Impulse);
}
So how would I go about adding torque to a rigidbody to bring it from any degrees/second to 0?
Note: I wrote this before looking up the different ForceModes, but all of the advice still stands. Your freeze was because you were asking for too much precision (you're subtracting from velocity by the same amount repeatedly, with no consideration for overstepping (.5 -1 is never 0) or direction (your object may not be spinning around Transform.up)), but I think what you really want follows:
The problem you're running into is that torque is to angular velocity like acceleration is to normal velocity - you can set your acceleration to billions, but that won't affect your velocity until some time passes. If you want to stop your object this frame, you really just want to set rigidBody.angularVelocity = Vector3.zero;. If you want to slow down your object over time, then AddTorque is what you're looking for, but you want to do it only once per frame.
If you're looking to make something slow down as if because of friction, then constant torque each frame is the goal, e.g.
float SlowDownPerSecond = 2f;
//...each frame:
if(rigidBody.angularVelocity.maginitude > SlowDownPerSecond * Time.deltaTime){
rigidBody.AddTorque(-rigidBody.angularVelocity.normalized * SlowDownPerSecond, ForceMode.Force);
} else {//less than change per frame
rigidBody.angularVelocity = Vector3.zero;
}
But you might also be less concerned with the velocity being exactly zero than you are with making it slow down smoothly, in which case you can just do
rigidBody.AddTorque(rigidBody.angularVelocity * -.05f, ForceMode.Force);
//or
rigidBody.AddTorque(rigidBody.angularVelocity * -.05f * Time.deltaTime, ForceMode.Impulse);
each frame. Although if this is what you're looking for, I think you can just up the angular Drag of the object either in the editor or at runtime with rigidbody.angularDrag = .1f, or whatever value you find feels right.

Get the position behind an object (locally) ? - Unity3D

I ran into a problem while making my complex(ish) camera behaviour (using Javascript). The problem that i have run to is that i am not able to get the position behind my player. Please don't tell me to use Vector3.back etc.. , because i want the position locally, to support turning. It won't work if the camera is always set to that position, cos i have a cool movement system in place.
I tried a number of approaches, and confused myself with the math. I also tried complex addition and subtraction. None of them worked at all.
I guess i am probably missing something quite simple, like a way to get into local coordinates etc.. ( or maybe a few math functions )
thanks in advance
-Etaash
You can get the forward vector of any transform, and if you negate that it is the backward vector. So on a script attached to the player you would have:
Vector3 backward = -transform.forward;
So to get a position, you could do something like this:
Vector3 pos = transform.position + (backward * dist); // where dist is a float to scale how far away from the position you want it