Search Unity

pulling an object off the ground and containing it

Discussion in 'Physics' started by steveh2112, Jan 25, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i want to be able to pull objects towards the playing and the following code is working fine except when there is an overhang between the player and object
    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         //Frame-rate independent for physics calculations and anything that runs at fixed rate.
    4.  
    5.         if (_PullAnimationIsPulling && _HitTargetGO)
    6.             PullHitTargetGOTowardsPlayersRWrist();
    7.     }
    8.     public void PullHitTargetGOTowardsPlayersRWrist()
    9.     {
    10.         if (_DistancePlayerToHitTarget <= _NewDistancePlayerToHitTarget)
    11.         {
    12.             ClearNewDistancePlayerToHitTarget(); // got there so stop moving
    13.             return;
    14.         }
    15.  
    16.         Rigidbody rb = _HitTargetGO.transform.GetComponent<Rigidbody>();
    17.  
    18.         const float speed = 60f;
    19.         Vector3 direction = _pc._wristR.position - _HitTargetGO.transform.position;
    20.         Vector3 movingStep = direction.normalized * speed;
    21.         //Debug.Log("PullRopeSMB movingStep=" + movingStep);
    22.         rb.AddForceAtPosition(movingStep, _ActiveArrow.transform.position, ForceMode.Impulse);
    23.     }
    the problem is if i am up on a ledge, i can pull the object towards me but after the effect of the impulse fades away, the object falls back to the ground.

    how would i constrain it so it stay stays say a fixed distance from the player?

    by the way, _ActiveArrow is a child of the thing i'm pulling (was parented when i hit the thing) and the point at which i want the thing constrained. maybe i can somehow freeze the position of that?
    thanks

    ops, i meant the title to say constraining it, can't fix
     
    Last edited: Jan 25, 2020
  2. PaulJohnJeffs

    PaulJohnJeffs

    Joined:
    Sep 7, 2017
    Posts:
    2
    There are a bunch of ways you could do it:

    - You could activate the constraints on the RigidBody component.
    - You could disable gravity on the object and set its velocity to zero. You could then keep updating the object's position to match a target transform if the point of constraint is moving
    - You could write a PID script and give it the desired location, for a smoother moving constraint.

    Not sure if that answers your question, as its a little hard to decipher exactly what your setup is, but hopefully it provokes some thought.