Movetowards Jerky motion; plays animation, then moves
I have a gameObject: enemy, these are spawned on the fly I have created several waypoints along a 2D map. The enemy object, once spawned, is told to go to each waypoint in order before leaving the screen, the movement towards the waypoints is fine, it follows through correctly, however the movement is awful, in the video tutorial I am using the movement is smooth and there are no issues, from what I can see, the motion happens like this:
Enemy is spawned Enemy plays default
'Walk Animation' Movetowards seems to
be called and the sprite
'jumps/teleports' to its new position
Enemy plays walk Animation
Movetowards is called.... and so on
and so on
I have tried changing the speed of Movetowards, and I can get the animation and speed to run in sync, but the overall speed of the object in sync is too slow for the game to play.
I have tried looking for answers to this but all seem to suggest using 'Movetwards' instead of transform.
Here is a selection of my code that I think is the issue (public variables defined in unity are shown in bold) GameManager.cs void Update () { if(waypoints != null) { navigationTime += Time.deltaTime; if(navigationTime > navigationUpdate) { if (target < waypoints.Length) { print("Target = " + target); gameObject.transform.position = Vector2.MoveTowards(current: transform.position, target: waypoints[target].transform.position, maxDistanceDelta:navigationTime );
} else {
gameObject.transform.position = Vector2.MoveTowards(current: transform.position, target: **exitpoint**.transform.position, maxDistanceDelta: navigationTime);
}
navigationTime = 0;
}
}
}
I think it may have something to do with adding 'position' should the entire transform object be applied to the new vector2 object?
e.g.
gameObject.transform = Vector2.MoveTowards(current: transform, target: **waypoints**[target].transform., maxDistanceDelta:3f * Time.deltaTime);
I'm unsure but it seems that leaving out 'position' (as was done in the video) seems to would cause an error, but I am a C# learner, I understand the framework and how to program, Im just learning the Grammar of C# as opposed to PHP (which I am proficient in).
Please if anyone can see the issue, please give me a point in the right direction!
Thank you in Advance.
Answer by Jimmyborofan · May 29, 2018 at 05:27 PM
So after a bit of looking and understanding exactly what was going on , I noticed that the navigationTime would only ever be higher than '1' as it was passed across, this meant that the sprite was moving in chunks of '1' unit at a time.
I also changed the gameObject to the enemy object by using
void Start () {
enemy = GetComponent<Transform>();
}
Once I had the instance rather than using generic 'this' I think this opened other methods that were used (though I am unsure about that atm) I then used the enemy transform instance to apply the transform changes, I also looked at the movement and why it was moving in chunks, To fix it I made the conditional a lessThan and moved the reset of NavigationTime variable (it is a class attribute, float value = 0.0f ) to outside of the conditional so that the sprite would move in smaller increments, then once it hit the '1' unit (sometimes more, depending upon the delta.TimeValue) it reset itself whilst comparing its distance to the next waypoint.
NavigationUpdate is a value that by default is set to 1; I wonder if this could be changed in order to control speed? Anyway, The final method looked like this:
void Update () {
if(waypoints != null) {
**navigationTime** += Time.deltaTime;
if(**navigationTime** < **navigationUpdate**) {
if (target < waypoints.Length) {
enemy.position = Vector2.MoveTowards(current: transform.position,
target: waypoints[target].transform.position, maxDistanceDelta:**navigationTime**);
} else {
enemy.position = Vector2.MoveTowards(current: transform.position,
target: exitpoint.transform.position, maxDistanceDelta: **navigationTime**);
}
}
**navigationTime** = 0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612163643im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Im using the HQ Fighting Animation FREE pack and have a problem with the end of the animations 0 Answers
Player's God animator vs separate animators? 1 Answer
animator.play() not working anymore 0 Answers
My animations started acting weirdly 0 Answers
Animator sets Default State everytime i switch an object's parent 0 Answers