Search Unity

Question Elastic Effect with SmoothDamp, Physics, and DOTween

Discussion in 'Physics' started by patrick_murphy_, May 18, 2023.

  1. patrick_murphy_

    patrick_murphy_

    Joined:
    Apr 16, 2017
    Posts:
    24
    I have somewhat of a complex set up for a feature. I would post the code but it's spread across 4 classes at this point, and is longer than anyone might want to read. So a description instead:

    Essentially I am trying to create a parabolic arc effect on a flicked object (without gravity, as you'll see).

    1. I am using DOTween to move an object along the Z-axis with easing, with a duration of 0.5f. Actually, it is DOVirtual.Vector3, so that the value is available to SmoothDamp and these don't fight each other and cause jitter.
    2. The user flicks an object with rigidbody along the X/Y axes, and when it is released:

    - the rigidbody has a force proportionate to the speed of the flick applied to X/Y axes
    - the DOTween is started to update the target position along the Z-axis
    - a SmoothDamp routine is enabled in FixedUpdate (to play nice with the rigidbody in motion) to create an elastic effect, and return the object to the moving transform along the Z-axis that DOVirtual is updating.

    The issue is that the object never makes it back to the x,y origin, and comes to rest before completing the arc.

    My guess is that SmoothDamp and non-kinematic rigidbodies don't play well together?

    Should I simulate the arc without involving physics (I don't want to do this because I have a lot of future ideas involving physics)?
    Alternatively, is it possible to set gravity to a Vector3 value, and then I can ditch the SmoothDamp logic, and turn on gravity for the rigidbody?

    Edit: I see it's possible to set gravity, but for the entire scene... which of course is reasonable, but I think I would need gravity set to a line, because multiple objects could be launched, which would all have to have their own independent elastic return to X = 0, Y = 0, but Z would be different for each object in motion.

    I know this is bizarre. Sorry!
     
    Last edited: May 18, 2023
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    Instead of gravity fortnehme whole scene you can use
    rb.AddForce(yourGravity, ForceMode.Acceleration)
    , but to be honest I don’t really know what you’re trying to achieve. Maybe use a spring joint to get the object back to the initial position? Sorry, just pointers, I didn’t get the whole idea :)
     
    patrick_murphy_ likes this.
  3. patrick_murphy_

    patrick_murphy_

    Joined:
    Apr 16, 2017
    Posts:
    24
    Yeah it's a bit of a complex idea, but I do appreciate your response. I will play with AddForce today and see if I can rig something up.