void FixedUpdate() { Vector3 vTarget = m_pathFindingBehaviour.TargetPos; //vTarget = RpgMapHelper.GetTileCenterPosition(vTarget); vTarget.z = transform.position.z; // stop when target position has been reached Vector3 vDist = (vTarget - transform.position); Debug.DrawLine(vTarget, transform.position); //TODO: the target is the touch position, not the target tile center. Fix this to go to target position once in the target tile if (m_pathFindingBehaviour.IsComputingPath) { ;// do nothing } else { bool isInTargetTile = RpgMapHelper.GetTileIdxByPosition(vTarget) == RpgMapHelper.GetTileIdxByPosition(transform.position); if (isInTargetTile || m_pathFindingBehaviour.CurrentPathNode != null && m_pathFindingBehaviour.CurrentPathNode.Next == null) { m_pathFindingBehaviour.enabled = false; if (vDist.magnitude > MinDistToReachTarget) { m_moving.Arrive(vTarget); } else { m_moving.Veloc = Vector3.zero; } } } UpdateAnimDir(); }
// Update is called once per frame void Update() { Vector3 vPlayerPos = m_player.transform.position; vPlayerPos.z = transform.position.z; Ray2D sightRay = new Ray2D(transform.position, vPlayerPos - transform.position); float distToTarget = Vector2.Distance(vPlayerPos, transform.position); float fSightBlockedDist = IsSightBlockedByBlockedTiles? RpgMapHelper.Raycast(sightRay, distToTarget) : -1f; // NOTE: fSightBlockedDist will be -1f if sight line is not blocked by blocked collision tile bool isPlayerSeen = distToTarget < SightDistance && fSightBlockedDist == -1f; if (isPlayerSeen) { m_pathFindingBehaviour.TargetPos = vPlayerPos; } bool isTargetReached = Vector2.Distance(m_pathFindingBehaviour.TargetPos, transform.position) <= MinDistToReachTarget; if (!isPlayerSeen && isTargetReached) { // Move around m_pathFindingBehaviour.enabled = false; vPlayerPos = transform.position; m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vPlayerPos += vOffset; m_moving.Arrive(vPlayerPos); } else // Follow the player { // stop following the path when closed enough to target m_pathFindingBehaviour.enabled = !isTargetReached; if (!m_pathFindingBehaviour.enabled) { m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vPlayerPos += vOffset; Debug.DrawLine(transform.position, m_player.transform.position, Color.blue); Debug.DrawRay(m_player.transform.position, vOffset, Color.blue); m_moving.Arrive(vPlayerPos); } } //+++avoid obstacles Vector3 vTurnVel = Vector3.zero; if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.RIGHT)) { vTurnVel.x = -m_moving.MaxSpeed; } else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.LEFT)) { vTurnVel.x = m_moving.MaxSpeed; } if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.DOWN)) { vTurnVel.y = m_moving.MaxSpeed; } else if (0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.UP)) { vTurnVel.y = -m_moving.MaxSpeed; } if (vTurnVel != Vector3.zero) { m_moving.ApplyForce(vTurnVel - m_moving.Veloc); } //--- //fix to avoid flickering of the creature when collides with wall if (Time.frameCount % 16 == 0) //--- { if (!LockAnimDir) { UpdateAnimDir(); } } }
protected void FixedUpdate() { if (m_curNode != null) { MapTileNode curTileNode = m_curNode.Value as MapTileNode; AutoTile autoTile = RpgMapHelper.GetAutoTileByPosition(transform.position, 0); if ( (autoTile.TileX != curTileNode.TileX || autoTile.TileY != curTileNode.TileY) || GetDistToTarget(autoTile) > MinDistToMoveNextTarget // wait until min dist is reached ) { Vector3 vSeek = curTileNode.Position; vSeek.z = transform.position.z; // put at this object level if (m_movingBehavior) { if (m_curNode.Next != null) { m_movingBehavior.Seek(vSeek); } else { m_movingBehavior.Arrive(vSeek); } } } else { m_curNode = m_curNode.Next; } } //if (TargetPos != null) //TODO: TargetPos can't be null { int prevTileidx = m_startTileIdx; m_startTileIdx = RpgMapHelper.GetTileIdxByPosition(TargetPos); m_isUpdatePath |= prevTileidx != m_startTileIdx; if (m_isUpdatePath)//|| !m_isComputing) //Removed to keep actor moving until reach the center of the node { m_isUpdatePath = false; m_endTileIdx = RpgMapHelper.GetTileIdxByPosition(transform.position); //now = Time.realtimeSinceStartup; StopCoroutine("ComputePath"); StartCoroutine("ComputePath"); } } //+++ Debug for (LinkedListNode <IPathNode> it = Path.First; it != null; it = it.Next) { MapTileNode mapTileNode0 = it.Value as MapTileNode; if (it.Next != null) { MapTileNode mapTileNode1 = it.Next.Value as MapTileNode; Vector3 v0 = mapTileNode0.Position; Vector3 v1 = mapTileNode1.Position; v0.z = transform.position.z; v1.z = transform.position.z; Debug.DrawLine(v0, v1, Color.red); } } //--- }