Jerky Movement.
Hi, I have a simple cube and sphere set up on a navmesh and the cube must reach the sphere. I train the model and the cube does reach the sphere MOST of the times(90%) but its movements are jerky, they smoothen out when I increase the "Frame to skip" in the academy. Could anyone help? Here's my code: `using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class Nav : Agent{ [SerializeField] GameObject cube, ball; NavMeshAgent nagent;
public float Xcod { get; private set; }
public float Xtarget { get; private set; }
public float Ztarget { get; private set; }
public float Zcod { get; private set; }
public override List<float> CollectState()
{
List<float> state = new List<float>();
state.Add(Xtarget);
state.Add(Xcod);
state.Add(Ztarget);
state.Add(Zcod);
return state;
}
public override void AgentReset()
{
Xtarget = Random.Range(-10,10);
Xcod = 0;
Zcod = 0;
Ztarget = Random.Range(-10, 10);
}
public override void AgentStep(float[] action)
{
ball.transform.position = new Vector3(Xtarget,0,Ztarget);
cube.transform.position = new Vector3(Xcod, 0.5f, Zcod);
nagent = cube.GetComponent<NavMeshAgent>();
switch ((int) action[0]) {
case 0:
Xcod -=0.1f;
reward = -0.1f;
break;
case 1:
Xcod += 0.1f;
reward = -0.1f;
break;
case 2:
Zcod +=0.1f;
reward = -0.1f;
break;
case 3:
Zcod -=0.1f;
reward = -0.1f;
break;
}
if (Vector3.Distance(cube.transform.position, ball.transform.position) > 10)
{
reward = -1;
done = true;
return;
}
if (Vector3.Distance(cube.transform.position, ball.transform.position) > 5)
{
reward = -0.1f;
}
if (Vector3.Distance(cube.transform.position, ball.transform.position) < 5)
{
reward = 0.1f;
}
if (Vector3.Distance(cube.transform.position, ball.transform.position) < 3)
{
reward = 1f;
done = true;
return;
}
nagent.SetDestination(new Vector3( Xcod, 0, Zcod));
}
} ` and yes I know moving and setting distances on a navmesh by 0.1f is expensive but it moved really fast even at 0.5f.
@awjuliani Any ideas?
Also its not just jerky movement.. sometimes the cube just stops moving forward or back it just sort of jerks around a spot and doesn't do much for quite sometime but then starts moving again after 30 to 40 seconds.
You implemented a behavior, which basically walks in four discrete directions. Combined with a high frequency of executing these actions, the navmesh agent won't walk "smoothly".
The NavMesh could help out to solve your issue. I suggest to scale up Xcod and Zcod and make the execution less frequent (like you already mentioned). I can imagine that your navmesh agent might reach its destination before the next step.
Using continuous actions instead of discrete ones can help as well. Or you can implement your movement execution differently. Maybe check out steering behaviors for this concern.
Hi! Thanks for the quick reply. I did try increasing the Xcod and the Zcod to 1 and more but it moves too quickly, even when I set the time scale to 0.1f the cube "teleports" from its current position to its target. If there's a way to do it with navmesh please let me know because the steering behaviours is a bit too complicated 😥
Hi, just an update. Increasing the penalty on the movement and smoothening the navigation by using Time.deltaTime helps to smoothen out the jerky movements but now the cube gets caught in looping motions and the only way out is to change the Frames to skip parameter in the Academy. Also the "teleportation" issue was solved by decreasing the timescale to 1 it was previously 100. Oops... hehehe Here's the updated script: ` using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class Nav : Agent{ [SerializeField] GameObject cube, ball; NavMeshAgent nagent;
public float Xcod { get; private set; }
public float Xtarget { get; private set; }
public float Ztarget { get; private set; }
public float Zcod { get; private set; }
public override List<float> CollectState()
{
List<float> state = new List<float>();
state.Add(Xtarget);
state.Add(Xcod);
state.Add(Ztarget);
state.Add(Zcod);
return state;
}
public override void AgentReset()
{
Xtarget = Random.Range(-10,10);
Xcod = 0;
Zcod = 0;
Ztarget = Random.Range(-10, 10);
}
public override void AgentStep(float[] action)
{
ball.transform.position = new Vector3(Xtarget,0,Ztarget);
cube.transform.position = new Vector3(Xcod, 0.5f, Zcod);
nagent = cube.GetComponent<NavMeshAgent>();
switch ((int) action[0]) {
case 0:
Xcod -=0.1f;
reward = -0.5f;
break;
case 1:
Xcod += 0.1f;
reward = -0.5f;
break;
case 2:
Zcod +=0.1f;
reward = -0.5f;
break;
case 3:
Zcod -=0.1f;
reward = -0.5f;
break;
}
if (Vector3.Distance(cube.transform.position, ball.transform.position) > 10)
{
reward = -1;
done = true;
return;
}
if (Vector3.Distance(cube.transform.position, ball.transform.position) > 5)
{
reward = -0.1f;
}
if (Vector3.Distance(cube.transform.position, ball.transform.position) < 5)
{
reward = 0.1f;
}
if (Vector3.Distance(cube.transform.position, ball.transform.position) < 3)
{
reward = 1f;
done = true;
return;
}
nagent.SetDestination(new Vector3( Xcod, 0, Zcod)*Time.deltaTime*0.5f);
}
} `
Got it!!! Setting up Max Steps to 50 makes it run smooth as butter. Is this considered an acceptable solution or is it like a hot-fix ?
Which Max Steps parameter?
A time scale of 100 makes the whole simulation run much faster. Given that, most of the frames are not displayed.
The very first one present in a Template Academy script , right above wait time and frames to skip. Here Yup I didnt know that my time scale was 100 but after i noticed and changed it things were definitely better.
That parameter specifies the maximum number of steps until the academy and the agents are reset. So after 50 steps (which is relatively very small) AcademyReset() and AgaentReset() are triggered. It doesn't make sense to me that this affects the actual movement of your agent.
Hmmm regardless it does make everything smoother.... so maybe its just a hot fix. I'll keep experimenting and get back to you.
Hi @Aarsh-Singh-Vishen , are you still having trouble with this issue?
Not really. Thank you!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.