DOPunchPosition modifies z-position
« on: July 17, 2015, 04:03:49 AM »
Good day. I have several objects that are overlapping with each other in terms of position. I'm having a problem if I use the DOPunchPosition function such that, even though I have the same z value for the object's transform position and the punch function's direction, I'm still observing that the object changes in the z-position. Here's my code:

Code: [Select]
public void PunchSprite(Vector3 sourcePos, int distance = 5) {
if(this.tweener != null) {
this.tweener.Kill(true);
}

Vector3 monstPos = this.transform.position;
float angleRad = Mathf.PI + Mathf.Atan2(sourcePos.y - monstPos.y, sourcePos.x - monstPos.x);
float targetX = Mathf.Cos(angleRad);
float targetY = Mathf.Sin(angleRad);
Vector3 targetPos = new Vector3(targetX, targetY, monstPos.z);
this.tweener = this.transform.DOPunchPosition(targetPos, 0.2f, distance, 1, false);
}

What I would like is to use the punch function without the object chaning its z-position. Any suggestions on how to fix this? Thanks in advance.

Re: DOPunchPosition modifies z-position
« Reply #1 on: July 18, 2015, 10:11:27 PM »
Chain an OnComplete to set it back to the original value.

this.tweener = this.transform.DOPunchPosition(targetPos, 0.2f, distance, 1, false).OnComplete(()=>this.transform. position = monstPos);

Re: DOPunchPosition modifies z-position
« Reply #2 on: July 20, 2015, 07:34:32 AM »
I also need to maintain the z-position for the entire duration of the punch because it overlaps another sprite (z-wise) for a split second while in the punch motion. Is this possible?

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: DOPunchPosition modifies z-position
« Reply #3 on: August 04, 2015, 05:51:46 PM »
Hi and sorry for the late reply (and thanks jprocha101 for the help),


To maintain the Z, just write 0 for the Z value of the Punch.