private void Awake() { trans = transform; Vector3 cur_position = trans.position; trans.position = NavMeshHelper.MakeReasonablePos(cur_position); _last_time = Time.realtimeSinceStartup; }
public void MoveToTargetPostion(Vector3 targtetPosition, float speed) { /*if (_entity.GetState() != E_StateId.move) * EntityEventFactory.ChangeInEntityState(_entity, E_StateId.move);*/ Vector3 target = NavMeshHelper.MakeReasonablePos(targtetPosition); TargetPosMove pos_move = target_pos_move as TargetPosMove; pos_move.target = target; pos_move.speed = speed; _move_component = target_pos_move; }
public Vector3 move_direction = Vector3.zero; // 键盘目标方向 public bool OnMove(EntityMovement entity_movement, float dt) { if (entity_movement._base_entity.GetState() != E_StateId.move) { return(true); } // 1.是否到达目的地 //if (!_reach_target_pos) return; if (move_direction == Vector3.zero) { EntityEventFactory.ChangeInEntityState(entity_movement._base_entity, E_StateId.idle); return(true); } Vector3 cur_position = entity_movement.trans.position; //cur_position = NavMeshHelper.MakeReasonablePos(cur_position); //float target_height = NavMeshHelper.GetGroundHeightY(cur_position + NAVMESH_RADIUS * move_direction); //if (MathHelper.IsEqual(target_height, MovementConst.error_height)) // return; // 2.移动的距离 Vector3 distance = move_direction * dt * entity_movement.movespeed; //Vector3 normal = trans.forward.normalized * distance.magnitude; //Vector3 tp = Quaternion.Euler(move_direction)*distance; // 3.下一个位置 Vector3 next_pos = cur_position + distance; /* float height = NavMeshHelper.GetGroundHeightY(next_pos); * if (!MathHelper.IsEqual(height, MovementConst.error_height)) * next_pos.y = height;*/ next_pos = NavMeshHelper.MakeReasonablePos(next_pos); entity_movement.trans.position = next_pos; // _update_direction(); entity_movement.OnRotating(move_direction); move_direction = Vector3.zero; /*if (!NavMesh.CalculatePath(cur_position, next_pos, NavMesh.AllAreas, nav_path)) * { * return; * } * if (nav_path.corners.Length < 2) * { * if (nav_path.corners.Length == 1) //有时会寻路出一个点的情况,这个还没好好研究 * { * distance = nav_path.corners[0] - cur_position; * distance = distance.normalized * dt * 5; * next_pos = cur_position + distance; * trans.position = next_pos; * } * else * { * //m_movement = Vector3.zero; * * } * } * else * { * distance = nav_path.corners[1] - cur_position; //调整方向 * distance = distance.normalized * dt * 5; * * next_pos = cur_position + distance; * * trans.position = next_pos; * }*/ return(false); }