Hello Guest

Author Topic: tk2dcamera smooth drag  (Read 3962 times)

quangtu89

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
tk2dcamera smooth drag
« on: August 23, 2013, 09:37:13 am »
My script attach to tk2dcamera to drag camera axis x

Code: [Select]

public override void TouchDownOut (Vector2 position)
{
dragOrigin = new Vector3 (position.x, position.y, 0);
dragOrigin = camera.ScreenToWorldPoint(dragOrigin);
originTime = Time.time;
if(!isMove)
{
isMove = true;
}
}

public override void TouchMoveOut (Vector2 position)
{
Vector3 currentPos = new Vector3 (position.x, position.y, 0);
currentPos = camera.ScreenToWorldPoint(currentPos);
if(isMove)
{
if (Vector3.Distance(dragOrigin,currentPos) > 10f)
{
float amtToMove = 20f * Time.deltaTime;

LGController.state = LGController.State.Animation;
movePos = dragOrigin - currentPos;
movePos = new Vector3(movePos.x * amtToMove,movePos.y,movePos.z);
Vector3 newPos = transform.position + movePos;
newPos.y = this.transform.position.y;

transform.position = newPos;
}
}
}
public override void TouchUpOut (Vector2 position)
{
        if(isMove)
        {
            isMove = false;
        }
}

But it not smooth . Anyone help me ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dcamera smooth drag
« Reply #1 on: August 23, 2013, 10:57:51 am »
It tends to move a lot smoother if you move it in FixedUpdate. Its not obvious where you're calling this from. Also make it move by fixed amounts (i.e. chasing, but with a smooth function) to help smooth out the jerkiness.

quangtu89

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: tk2dcamera smooth drag
« Reply #2 on: August 27, 2013, 01:35:52 pm »
i use this script
Code: [Select]
void FixedUpdate()
{
         if(this.transform.position != targetPoint)
{
this.transform.position = Vector3.SmoothDamp(this.transform.position, targetPoint, ref unused, 0.15f, float.MaxValue, Time.deltaTime);
}
}
but i dont see move smooth
I think render sprite is trouble . I can see border in my sprite when camera move . I use sprite is a white square and i set color for its.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dcamera smooth drag
« Reply #3 on: August 27, 2013, 01:45:19 pm »
You should avoid moving the camera in sub-pixel positions if your textures aren't set up in a very specific way. If you're using 1 pixel per meter, then you can set position.x = Mathf.Round(position.x); to make sure its a pixel boundary.
You'll need a mirror Vector3 cameraPosition so you can still maintain the fragment position in FixedUpdate.