- Home /
Irrelevant, I am working towards a different solution
Move rigidbody to position and still have forces act on it
I am trying to create a system that lets the player pick up and carry around a rigidbody gun where it moves along with the player, while still reacting to collisions so that it can bump into walls and things like that.
I used the answer from here Stopping a rigidbody at target to make the gun move and stop when it gets to the position it needs to be. It works like it should, but I need to make it so that when the gun gets to the position, it will move along with the velocity of the player which is also a rigidbody.
So then I added the velocity of the player to the toVel variable, but now when I move around the gun is very jerky. Here is the code... Also a video showing my problem, see how it's jerky when moving side to side when aiming down sights... My video
And some big inspiration from this video... UE4 - Weapon collision vs world
void FixedUpdate ()
{
if(beingHeldByPlayer)
{
Vector3 dist = moveToPos - transform.position;
Vector3 tgtVel = Vector3.ClampMagnitude ((toVel * dist) + playerObj.GetComponent <Rigidbody>().velocity, maxVel) ;
Vector3 error = (tgtVel - rb.velocity);
Vector3 force = Vector3.ClampMagnitude ((gain * error), maxForce);
rb.AddForce (force);
}
}
I know I shouldn't use getcomponent in an update loop too, I'm just trying to get things working.
hello, i would try using vector3.movetowards
Vector3 tgtVel = Vector3.$$anonymous$$oveTowards(rb.velocity * (toVel * dist) + playerObj.GetComponent <Rigidbody>().velocity, maxVel) ;
you will have to addapt maxvel to this new method test it and tell me if it worked :)
@xxmariofer Using movetowards made it less jerky, but I still have the problem that when ai$$anonymous$$g down sights, it does not follow along with the player exactly, it still lags behind which is not ideal
hello, then what you are looking for is diviging the x and y velocitys. making the x y and z values "movetowards" at different velocitys, is the same method as my previous answer but you can use the $$anonymous$$athf.$$anonymous$$oveTowards to iterate all 3 velocitys separatly.
Follow this Question
Related Questions
Solar System Simulator Orbits Not Working 2 Answers
Rotate child object under RigidBody parent 0 Answers
Rigidbody climbing stairs 2 Answers
Can't set position of Rigidbody precisely 0 Answers
Physics update rate problems 1 Answer