1
\$\begingroup\$

When I press attack button, player will change to Normal Attack state to perform Normal Attack animation and you will notice the player seem like step back a little bit as the width of sprite for Normal Attack animation is longer than Idle animation.

http://youtu.be/hnPXt-kprqQ

To solve this issue. I need to change the transform.position.x =+ 0.5 to move a bit further to match the position of Idle animation stands and transform.position.x =- 0.5 when Normal Attack state done its animation and back to Idle state.

How can I perform transform.position.x =- 0.5 when when Normal Attack state done its animation and back to Idle state?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Unity won't allow you change a transforms components individually. You can create a new transform and initialize it accordingly, then assign it to game object.

Vector3 pos = transform.position;
Vector3 tmp(pos.x+0.5, pos.y, pos.z);
transform.position = tmp;
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You don't need the tmp. Just modify the components of pos and assign it back to transform.position. \$\endgroup\$
    – bummzack
    May 2, 2014 at 14:05
  • \$\begingroup\$ Yes. It was a shorthand. I better use comments next time :) \$\endgroup\$
    – Varaquilex
    Jan 19, 2015 at 20:12
1
\$\begingroup\$

Simpler yet is to use the built in Translate method. It allows for more complex movement, like moving relative to the camera or local space and is pretty simple to use:

transform.Translate(0.5, 0, 0, Space.World);

or

transform.Translate(-0.5, 0, 0, Space.World);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .