- Home /
Setting Transform.position inaccurate?
Hi guys,
I am working on a game with judo/ wrestling style grapples and throws. I've got two characters, each character has an animator, when one character hits the grapple button, I use the transform to place the receiver to be in the correct position. However, this method doesn't give the same results every time it's called. Sometimes the rotation will be wrong, sometimes the positioning. It's always kind of right, but never seems to be exactly where it should be, and the positioning never seems to be consistent.
Here's a snippet of my code.
Vector3 playerPos = attackerAnim.transform.position;
Vector3 playerDirection = attackerAnim.transform.forward;
Quaternion playerRotation = attackerAnim.transform.rotation;
if (attackerBattlestats.stamina > Moves.frontLight[movename][2]) {
float oppDistance = 0.5f;
Vector3 oppPos = playerPos + playerDirection * oppDistance;
oppAnim.transform.position = oppPos;
opponent.transform.LookAt(attacker.transform.position);
attacker.transform.LookAt(opponent.transform.position);
attackerAnim.SetInteger("LightGrappleAttackAnim", Moves.frontLight[movename][4]);
oppAnim.SetInteger("LightGrappleReactAnim", Moves.frontLight[movename][4]);
IEnumerator playMoveAtt = moveset.stopAnim("LightGrappleAttackAnim", attackerAnim);
StartCoroutine(playMoveAtt);
IEnumerator playMoveRec = moveset.stopAnim("LightGrappleReactAnim", oppAnim);
StartCoroutine(playMoveRec);
}
This is part of a bigger method, animators (oppAnim and attackerAnim) are passed in, The animator.SetInteger
moves the animator from anystate to the selected grapple. The StopAnim
IEnumerator changes the set integer back to 0, after the animation has started playing. The various BattleStates variables are just objects to hold characters health etc, also passed into the method.
I have a feeling it's because mecanim or something adds in some velocity/ momentum that changing the transform won't stop, but my google searches haven't confirmed this. There are no colliders, so I don't see why it isn't working.
Any help is much appreciated.
I can guess your grapple hook is child of something that is on character that's being animated? If so most likely it's $$anonymous$$ecanim changing transform constantly as it plays animation. Fixing is either unparenting it(might break something though) or setting transform after mecanim pass each frame (for example, LateUpdate()). I don't see where this method gets called: is it Update() or LateUpdate()? If it's Update() try moving it into LateUpdate(). If it's not called every frame, that's bad then as mecanim changes state each frame and you will need to update transform each frame.
Thanks @Teravisor. It was called in Update() (sorry for not saying!), I have tried moving it to LateUpdate(), and that has helped make it a lot more consistent, but it's still not perfect. It's not actually a grapple hook, rather two human characters going through a judo throw like animation, ie two people grappling each other. They are both child objects of their own spawn object, but the spawn object doesn't move.
Your answer
![](https://koobas.hobune.stream/wayback/20220612061506im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rig object offsets during animations 0 Answers
transform.position not setting position OR animation setting position even though it shouldn't 0 Answers
Rig Extra Transforms to Expose in Hierarchy for Bio IK 0 Answers
Animation issue (OnTriggerEnter) ***SOLVED*** 1 Answer
Transformation/Rotation of completed animation to fit on scene 1 Answer