Hello Guest

Author Topic: Smoothdamp  (Read 8033 times)

benswinden

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Smoothdamp
« on: August 03, 2013, 07:53:19 pm »
I'm using Vector3.SmoothDamp to do smooth camera movement with a tk2dCamera. As the camera moves, all my sprites jiggle slightly. Can you give any insight into why this is happening and how I can prevent it? Here is the code i'm using to move the camera.

Code: [Select]
Vector3 point = camera.WorldToViewportPoint(targetTransform.position);
Vector3 delta = targetTransform.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Smoothdamp
« Reply #1 on: August 03, 2013, 08:22:16 pm »
Are you doing this in FixedUpdate?

benswinden

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Smoothdamp
« Reply #2 on: August 03, 2013, 08:30:16 pm »
Nope

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Smoothdamp
« Reply #3 on: August 03, 2013, 08:41:20 pm »
You should try it in fixedupdate - this usually improves things massively.
Also check unity answers for solutions to this - its a common issue people encounter moving cameras in Unity and there are lots of solutions and workarounds you can find if fixedupdate isn't good enough for you.

profanicus

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 167
    • View Profile
Re: Smoothdamp
« Reply #4 on: August 03, 2013, 11:34:54 pm »
You should also try camera movement in LateUpdate. FixedUpdate didn't work so well for me back when I did my current camera follow, as it's called before Update and on a different timer, whereas LateUpdate will be called after all your objects have been Updated.

Also you could try changing SmoothDamp to a regular Lerp with a deltaTime, from memory I had troubles getting SmoothDamp to actually provide a smooth camera. :P
« Last Edit: August 04, 2013, 12:31:27 am by profanicus »

benswinden

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Smoothdamp
« Reply #5 on: August 04, 2013, 04:54:34 pm »
Thanks for the advice guys, i understand now that this isn't a tk2d issue so I appreciate you taking some time to help out.

Neither FixedUpdate or LateUpdate made any difference with the assets moving unfortunately. I also tried using Lerp rather than SmoothDamp however the movement it causes isn't smooth enough for what I need to use it for.

I'm unsure where to start looking on the forums in order to solve this problem since I really don't understand why moving the camera is causing the assets to shake the way they are. Any suggestions would be greatly appreciated.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Smoothdamp
« Reply #6 on: August 04, 2013, 05:35:32 pm »
The assets aren't shaking - they're not moving are they? Its just the camera movement causes the assets to appear jerky.
http://forum.unity3d.com/threads/130920-How-to-smooth-damp-towards-a-moving-target-without-causing-jitter-in-the-movement
Theres a long discussion about it there, and there are loads more discussion on answers.unity3d.com - camera jitter might be a good place to start?