// Update is called once per frame void FixedUpdate() { // Move the player towards the destination if (running && agent != null && character.enabled) { // Move the character using unity's physics // Reset the position of the character to the nav agent's position if they are too far away { var resetDistanceThreshold = 4; var distanceToNavAgent = (transform.position - ToV3(agent.Position)).magnitude; if (distanceToNavAgent > resetDistanceThreshold) { transform.position = ToV3(agent.Position); } } { var svelocity = agent.Vel; var velocity = ToV3(svelocity); if (!character.isGrounded) { velocity += new Vector3(0, gravity, 0); } bool moved = false; //character.SimpleMove(velocity); if (!moved) { var delta = velocity * Time.fixedDeltaTime; character.Move(delta); } // Set the rotation var direction = velocity; direction.y = 0; var speedSq = direction.sqrMagnitude; if (speedSq > 0.01f) { transform.rotation = Quaternion.LookRotation(direction.normalized); } } try { // Move the nav agent var navPoint = navMesh.NavMeshQuery.FindNearestPoly(ToSV3(transform.position), ToSV3(Vector3.one * 2)); agent.Position = navPoint.Position; navPoint = navMesh.NavMeshQuery.FindNearestPoly(ToSV3(destination), ToSV3(Vector3.one * 2)); agent.RequestMoveTarget(navPoint.Polygon, navPoint.Position); } catch (System.Exception) { //Debug.Log(e); } } }
// Update is called once per frame void FixedUpdate() { // Move the player towards the destination if (running && agent != null) { // Move the character using unity's physics // Reset the position of the character to the nav agent's position if they are too far away { var resetDistanceThreshold = 4; var distanceToNavAgent = (ActorPosition3D - ToV3(agent.Position)).magnitude; if (distanceToNavAgent > resetDistanceThreshold) { ActorPosition3D = ToV3(agent.Position); } } { var svelocity = agent.Vel; var velocity = ToV3(svelocity); rigidBody2D.velocity = FlipYZ(velocity); if (velocity.sqrMagnitude > 0.01f) { previousDirection = velocity.normalized; } } try { // Move the nav agent var navPoint = navMesh.NavMeshQuery.FindNearestPoly(ToSV3(ActorPosition3D), ToSV3(Vector3.one * 2)); agent.Position = navPoint.Position; navPoint = navMesh.NavMeshQuery.FindNearestPoly(ToSV3(FlipYZ(destination)), ToSV3(Vector3.one * 2)); agent.RequestMoveTarget(navPoint.Polygon, navPoint.Position); _debugNavDest = ToV3(navPoint.Position); } catch (System.Exception) { //Debug.Log(e); } } }