- Home /
orienting navmesh to the normal surface of the ground experiences jerky motion
Hello devs, how are you doing, i'm facing a bit odd problem here, I'm using navmesh agent on my traffic car which has to move upwards and downwards on elevated track paved on terrain, I'm calculating normal of the surface and orienting my car with the normal of the surface, it does get oriented to the surface of the road but when ever it orients to the level of the surface,it experiences some jerky motion
I'm using quaternion.slerp for rotating vehicle from its current orientation to the level of the normal calculated, but i see no impact of as if during play mode while playing is paused, i drag the vehicle above the surface in the scene view and rotate it , and when i move the next frame, all of the sudden it gets oriented to the level of the ground, means no interpolations based on time.
using UnityEngine;
using System.Collections;
public class TerribleAi : MonoBehaviour {
public Transform[] destinations;
NavMeshAgent Agenti;
public int destinationcounter = 0;
public int initialwaypoint = 0;
public RCCAIWaypointsContainer waypoints;
// Use this for initialization
void Start () {
destinations = waypoints.waypoints.ToArray();
Agenti = gameObject.GetComponent<NavMeshAgent>();
Agenti.SetDestination(destinations[destinationcounter].position);
}
Vector3 temp;
Vector3 myrotation;
Quaternion rotation;
float distance;
// Update is called once per frame
void Update () {
RaycastHit hit;
Debug.DrawRay(transform.position, -transform.up * 5f, Color.red);
if (Physics.Raycast(transform.position, -transform.up, out hit, 2f))
{
if ( hit.transform.gameObject.layer == 8)
{
temp = Vector3.Cross(hit.normal, transform.forward);
myrotation = Vector3.Cross(temp, hit.normal)*Time.deltaTime;
rotation = Quaternion.LookRotation(myrotation);
// transform.rotation = rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 3f);
Debug.Log("Applying rotation");
}
}
distance = Vector3.Distance(gameObject.transform.position, destinations[destinationcounter].position);
if (distance <=2f)
{
destinationcounter++;
Agenti.SetDestination(destinations[destinationcounter].position);
}
it experiences jerky movement while going down the hill. what wrong am i doing here?
Answer by FortisVenaliter · May 04, 2017 at 08:15 PM
A couple things stand out to me:
You're multiplying the 'myrotation' variable by Time.deltaTime, but that only multiplies it's magnitude... it should be a unit vector for what it's used for.
In your Slerp call, you use 3f for the value of t. But the docs say that should be clamped to [0,1]. The reason is because 1 means instant change, while zero means no change. So, by setting it above one, you're basically making the call redundant.
@FortisVenaliter thanks for replying in details, I looked into my script and altered it, I'm not multiplying myrotation by Time.deltaTime any more, i also decremented the the value of t in slerp to 1 and tried 0.5 even still no luck.
the slope is continuous, can it happen due to lag in applying the calculated rotation to the current rotation, i mean, by the time the calculated rotation is applied, the vehicle is translated to another position for which the normal of the surface might differ from the previous one, I'm sorry if it makes no sense, how ever i tried using Rotatetowards, calculated angle between current rotation and calculated rotation and supplied that as a maxDeltaangle to Rotatetowards but no luck..
I would almost reduce it further, to 0.1-0.2. Can you post a video of the problem?
Thanks @FortisVenaliter the problem has been resolved by making an object a child of an agent and applying calculated rotation to it.
I am having a similar problem and I suspect it is due to the fact that as the rotation or the object changes, so does it's raycast angle down from it to the mesh below. I believe in my case that the raycast sweeps across two different normals back and forth and keeps orienting itself to compensate for the change.
I figured out the navmesh agent takes care of it's rotation on it's own the one i calculated each frame was in conflict with the rotation calculated by agent on it's own, the best workaround I discovered is to have an actual object as a child of an agent, an agent could be anything, cube or sphere, and then ins$$anonymous$$d of applying rotation to the agent itself we apply rotation to the child of an agent and the jerky phenomenon takes place no more.
Your answer
Follow this Question
Related Questions
NavMeshAgent Not Working? 1 Answer
Tank not moving towards player 1 Answer
Make NavMesh move to random positions 1 Answer
Unwalkable nav mesh areas based on height map? 0 Answers
How NavMesh Cost can slow down NavMeshAgent's speed? 0 Answers