- Home /
Why do my Navmesh Agents pop through the center axis of the mesh?
I have a simple plane that I've generated a mesh for and two capsules with Navmesh agents attached. They move around just fine, but when moving right along the Z-Axis or X-Axis (Which are lined up with the Global Z and X Axis) the capsules will glitch up and down but still move for the most part. Occasionally they will full glitch out.
I get this error on the camera, but I'm not really surprised by this one.
Screen position out of view frustum (screen pos 917.000000, 802.000000) (Camera rect 0 0 982 552) UnityEngine.Camera:ScreenPointToRay(Vector3) PlayerController:RotatePlayer(Single, Single) (at Assets/Scripts/PlayerController.cs:61) PlayerController:PlayerMovement(Single, Single) (at Assets/Scripts/PlayerController.cs:39) PlayerController:FixedUpdate() (at Assets/Scripts/PlayerController.cs:29)
Here's a video I made to demonstrate what is happening.
The Capsules have Navmesh Agent components and Rigidbodies. Here is the code I use to move the player.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
private int jumpState = 0;
public float speedScale = 1f;
public float jumpHeight = 1f;
// public float turnSmoothing = 15f;
private bool isGrounded;
private Rigidbody rb;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate ()
{
// Horizontal movement
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
PlayerMovement(x, z);
}
void PlayerMovement(float h, float v)
{
if(h != 0f || v != 0f)
{
MovePlayer(h, v);
}
RotatePlayer(h, v);
if(Input.GetButton("Jump") && (jumpState < 10))
Jump();
}
void MovePlayer(float h, float v)
{
Vector3 targetPos = new Vector3(h * speedScale, 0f, v * speedScale);
rb.MovePosition(transform.position + (targetPos * Time.deltaTime));
}
void RotatePlayer(float h, float v)
{
/*
Vector3 targetDirection = new Vector3(h, 0f, v);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion smoothedRot = Quaternion.Lerp(rb.rotation, targetRotation, (turnSmoothing * Time.deltaTime));
rb.MoveRotation(smoothedRot);
*/
// Create a ray from the mouse cursor on screen in the direction of the camera.
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
// Create a RaycastHit variable to store information about what was hit by the ray.
RaycastHit floorHit;
// Perform the raycast and if it hits something on the floor layer...
if(Physics.Raycast (camRay, out floorHit, 300f, 1 << 8))
{
transform.LookAt(new Vector3(floorHit.point.x, transform.position.y, floorHit.point.z));
}
}
void Jump()
{
rb.AddForce(Vector3.up * jumpHeight);
if(!isGrounded)
jumpState++;
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Ground")
{
isGrounded = true;
jumpState = 0;
}
}
void OnCollisionExit(Collision collision)
{
if(collision.gameObject.tag == "Ground")
isGrounded = false;
}
}