void Update() { // copy properties to projection all the time // (in case they are modified after creating it) agent.speed = speed; agent.angularSpeed = angularSpeed; agent.acceleration = acceleration; agent.stoppingDistance = stoppingDistance; agent.autoBraking = autoBraking; agent.radius = radius; agent.obstacleAvoidanceType = quality; agent.avoidancePriority = priority; agent.autoRepath = autoRepath; // copy position: transform in Update, rigidbody in FixedUpdate if (rigidbody2D == null || rigidbody2D.isKinematic) { transform.position = NavMeshUtils2D.ProjectTo2D(agent.transform.position); } // stuck detection if (IsStuck()) { // stop agent movement, reset it to current position agent.ResetPath(); agent.transform.position = NavMeshUtils2D.ProjectTo3D(transform.position); Debug.Log("stopped agent because of collision in 2D plane"); } }
// monobehaviour /////////////////////////////////////////////////////////// void Awake() { // create projection var go = GameObject.CreatePrimitive(PrimitiveType.Cylinder); go.name = "NAVIGATION2D_AGENT"; go.transform.position = NavMeshUtils2D.ProjectTo3D(transform.position); // todo height 0.5 again? agent = go.AddComponent <NavMeshAgent>(); rigidbody2D = GetComponent <Rigidbody2D>(); collider2D = GetComponent <Collider2D>(); // disable navmesh and collider (no collider for now...) Destroy(agent.GetComponent <Collider>()); Destroy(agent.GetComponent <MeshRenderer>()); // pass serialized values once agent.speed = _speed; agent.angularSpeed = _angularSpeed; agent.acceleration = _acceleration; agent.stoppingDistance = _stoppingDistance; agent.autoBraking = _autoBraking; agent.radius = _radius; agent.obstacleAvoidanceType = _quality; agent.avoidancePriority = _priority; agent.autoRepath = _autoRepath; }
public void endRoll() { anim.SetBool("Roll", false); rb.velocity = Vector2.zero; nav.agent.transform.position = NavMeshUtils2D.ProjectTo3D(transform.root.position); nav.enabled = true; roll = false; nav.destination = dest.position; }
public void Warp(Vector2 v) { // try to warp, set this agent's position immediately if it worked, so // that Update doesn't cause issues when trying to move the rigidbody to // a far away position etc. if (agent.Warp(NavMeshUtils2D.ProjectTo3D(v))) { transform.position = v; } }
// monobehaviour /////////////////////////////////////////////////////////// void Awake() { // create projection var go = GameObject.CreatePrimitive(PrimitiveType.Cylinder); go.name = "NAVIGATION2D_AGENT"; go.transform.position = NavMeshUtils2D.ProjectTo3D(transform.position); // todo height 0.5 again? agent = go.AddComponent <NavMeshAgent>(); // disable navmesh and collider (no collider for now...) Destroy(agent.GetComponent <Collider>()); Destroy(agent.GetComponent <MeshRenderer>()); }
public bool CalculatePath(Vector2 targetPosition, NavMeshPath2D path) { var temp = new NavMeshPath(); if (agent.CalculatePath(NavMeshUtils2D.ProjectTo3D(targetPosition), temp)) { // convert 3D to 2D path.corners = temp.corners.Select(NavMeshUtils2D.ProjectTo2D).ToArray(); path.status = temp.status; return(true); } return(false); }
public bool CalculatePath(Vector2 targetPosition) { var temp = new NavMeshPath(); if (agent.CalculatePath(NavMeshUtils2D.ProjectTo3D(targetPosition), temp)) { return(true); } else { return(false); } }
// monobehaviour /////////////////////////////////////////////////////////// void Awake() { // create projection var go = GameObject.CreatePrimitive(PrimitiveType.Cylinder); go.name = "NAVIGATION2D_OBSTACLE"; go.transform.position = NavMeshUtils2D.ProjectTo3D(transform.position); go.transform.rotation = Quaternion.Euler(NavMeshUtils2D.RotationTo3D(transform.eulerAngles)); obst = go.AddComponent <NavMeshObstacle>(); // disable mesh and collider (no collider for now) Destroy(obst.GetComponent <Collider>()); Destroy(obst.GetComponent <MeshRenderer>()); }
void Update() { // copy properties to projection all the time // (in case they are modified after creating it) obst.carving = carve; obst.center = NavMeshUtils2D.ProjectTo3D(center); obst.size = new Vector3(size.x, 1, size.y); // scale and rotate to match scaled/rotated sprites center properly obst.transform.localScale = new Vector3(transform.localScale.x, 1, transform.localScale.y); obst.transform.rotation = Quaternion.Euler(NavMeshUtils2D.RotationTo3D(transform.eulerAngles)); // project position to 3d obst.transform.position = NavMeshUtils2D.ProjectTo3D(transform.position); }
void FixedUpdate() { // copy properties to projection all the time // (in case they are modified after creating it) agent.speed = speed; agent.angularSpeed = angularSpeed; agent.acceleration = acceleration; agent.stoppingDistance = stoppingDistance; agent.autoBraking = autoBraking; agent.radius = radius; agent.obstacleAvoidanceType = quality; agent.avoidancePriority = priority; agent.autoRepath = autoRepath; // copy projection's position var rb = GetComponent <Rigidbody2D>(); if (rb != null && !rb.isKinematic) { rb.MovePosition(NavMeshUtils2D.ProjectTo2D(agent.transform.position)); } else { transform.position = NavMeshUtils2D.ProjectTo2D(agent.transform.position); } // stuck detection: get max distance first (best with collider) float maxdist = 2; // default if no collider if (GetComponent <Collider2D>()) { var bounds = GetComponent <Collider2D>().bounds; maxdist = Mathf.Max(bounds.extents.x, bounds.extents.y) * 2; } // stuck detection: reset if distance > max distance float dist = Vector2.Distance(transform.position, NavMeshUtils2D.ProjectTo2D(agent.transform.position)); if (dist > maxdist) { // stop agent movement, reset it to current position agent.ResetPath(); agent.transform.position = NavMeshUtils2D.ProjectTo3D(transform.position); Debug.Log("stopped agent because of collision in 2D plane"); } }
void Update() { // copy position: transform in Update, rigidbody in FixedUpdate if (rigidbody2D == null || rigidbody2D.isKinematic) { transform.position = NavMeshUtils2D.ProjectTo2D(agent.transform.position); } // stuck detection if (IsStuck()) { // stop agent movement, reset it to current position agent.ResetPath(); agent.transform.position = NavMeshUtils2D.ProjectTo3D(transform.position); Debug.Log("stopped agent because of collision in 2D plane"); } }
// based on: https://docs.unity3d.com/ScriptReference/AI.NavMesh.SamplePosition.html public static bool SamplePosition(Vector2 sourcePosition, out NavMeshHit2D hit, float maxDistance, int areaMask) { NavMeshHit hit3D; if (NavMesh.SamplePosition(NavMeshUtils2D.ProjectTo3D(sourcePosition), out hit3D, maxDistance, areaMask)) { hit = new NavMeshHit2D { position = NavMeshUtils2D.ProjectTo2D(hit3D.position), normal = NavMeshUtils2D.ProjectTo2D(hit3D.normal), distance = hit3D.distance, mask = hit3D.mask, hit = hit3D.hit }; return(true); } hit = new NavMeshHit2D(); return(false); }
public static bool Raycast(Vector2 sourcePosition, Vector2 targetPosition, out NavMeshHit2D hit, int areaMask) { if (NavMesh.Raycast(NavMeshUtils2D.ProjectTo3D(sourcePosition), NavMeshUtils2D.ProjectTo3D(targetPosition), out NavMeshHit hit3D, areaMask)) { hit = new NavMeshHit2D { position = NavMeshUtils2D.ProjectTo2D(hit3D.position), normal = NavMeshUtils2D.ProjectTo2D(hit3D.normal), distance = hit3D.distance, mask = hit3D.mask, hit = hit3D.hit }; return(true); } hit = new NavMeshHit2D(); return(false); }
public void ChangePath() { agent.ResetPath(); agent.transform.position = NavMeshUtils2D.ProjectTo3D(transform.position); }